Skip to content

Commit

Permalink
collect the server from environment variables (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
haim-kermany authored Dec 17, 2024
1 parent 1756bdc commit 0a16130
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
26 changes: 17 additions & 9 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ SPDX-License-Identifier: Apache-2.0
package main

import (
"fmt"
"os"
"strings"
"testing"
)

const noServerInfo = "--host no_host --username no_user --password no_password"
const serverInfo = "--host no_host --username no_user --password no_password"

func TestMain(t *testing.T) {
tests := []struct {
name string
Expand All @@ -31,11 +30,11 @@ func TestMain(t *testing.T) {
},
{
name: "collect-only",
args: serverInfo + " --resource-dump-file examples/output/resources.json --skip-analysis",
args: "--resource-dump-file examples/output/resources.json --skip-analysis",
},
{
name: "collect-anonymize",
args: serverInfo + " --resource-dump-file examples/output/resources_anon.json --skip-analysis --anonymize",
args: "--resource-dump-file examples/output/resources_anon.json --skip-analysis --anonymize",
},
{
name: "anonymize-only",
Expand Down Expand Up @@ -74,15 +73,24 @@ func TestMain(t *testing.T) {
},*/
{
name: "collect-and-analyze",
args: serverInfo + " --resource-dump-file examples/output/resources2.json --filename examples/output/analysis2.txt",
args: "--resource-dump-file examples/output/resources2.json --filename examples/output/analysis2.txt",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if strings.Contains(tt.args, resourceInputFileFlag) || !strings.Contains(tt.args, noServerInfo) {
if err := _main(splitArgs(tt.args)); err != nil {
t.Errorf("_main() error = %v,", err)
serverInfo := ""
if !strings.Contains(tt.args, resourceInputFileFlag) {
// you can set your server info here:
// serverInfo = "--host host --username user --password password "
if serverInfo == "" && os.Getenv("NSX_HOST") == "" {
fmt.Println("didn't got any server")
return
}
serverInfo =
fmt.Sprintf("--host %s --username %s --password %s ", os.Getenv("NSX_HOST"), os.Getenv("NSX_USER"), os.Getenv("NSX_PASSWORD"))
}
if err := _main(splitArgs(serverInfo + tt.args)); err != nil {
t.Errorf("_main() error = %v,", err)
}
})
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestCollectResources(t *testing.T) {
{
"simple",
args{
// you can set your server info here:
"no_server",
"no_user",
"no_password",
Expand All @@ -42,8 +43,11 @@ func TestCollectResources(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.args.nsxServer == "no_server" {
fmt.Println("didn't got any server")
return
if os.Getenv("NSX_HOST") == "" {
fmt.Println("didn't got any server")
return
}
tt.args = args{os.Getenv("NSX_HOST"), os.Getenv("NSX_USER"), os.Getenv("NSX_PASSWORD")}
}
server := NewServerData(tt.args.nsxServer, tt.args.userName, tt.args.password)
got, err := CollectResources(server)
Expand Down
9 changes: 7 additions & 2 deletions pkg/model/verify_trace_flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"fmt"
"os"
"path"
"strings"
"testing"
Expand All @@ -27,6 +28,7 @@ func Test_verifyTraceflow(t *testing.T) {
{
"simple",
args{
// you can set your server info here:
"no_server",
"no_user",
"no_password",
Expand All @@ -36,8 +38,11 @@ func Test_verifyTraceflow(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.args.nsxServer == "no_server" {
fmt.Println("didn't got any server")
return
if os.Getenv("NSX_HOST") == "" {
fmt.Println("didn't got any server")
return
}
tt.args = args{os.Getenv("NSX_HOST"), os.Getenv("NSX_USER"), os.Getenv("NSX_PASSWORD")}
}
server := collector.NewServerData(tt.args.nsxServer, tt.args.userName, tt.args.password)
got, err := collector.CollectResources(server)
Expand Down
8 changes: 6 additions & 2 deletions pkg/vsphrcoll/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package vsphrcoll

import (
"fmt"
"os"
"path"
"testing"

Expand Down Expand Up @@ -39,8 +40,11 @@ func TestCollectResources(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.args.server == "no_server" {
fmt.Println("didn't got any server")
return
if os.Getenv("NSX_HOST") == "" {
fmt.Println("didn't got any server")
return
}
tt.args = args{os.Getenv("VSPHERE_HOST"), os.Getenv("VSPHERE_USER"), os.Getenv("VSPHERE_PASSWORD")}
}
got, err := CollectResources(tt.args.server, tt.args.userName, tt.args.password)
if err != nil {
Expand Down

0 comments on commit 0a16130

Please sign in to comment.