Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cmd
import (
"code.fbi.h-da.de/cocsn/gosdn/cli"
guuid "github.com/google/uuid"
"github.com/spf13/viper"
"os"
"testing"
)
var testAddress = "141.100.70.171:6030"
var testApiEndpoint = "http://141.100.70.171:8080"
var testUsername = "admin"
var testPassword = "arista"
func testSetupIntegration() {
a := os.Getenv("GOSDN_TEST_ENDPOINT")
if a != "" {
testAddress = a
}
api := os.Getenv("GOSDN_TEST_API_ENDPOINT")
if api != "" {
testApiEndpoint = api
}
u := os.Getenv("GOSDN_TEST_USER")
if u != "" {
testUsername = u
}
p := os.Getenv("GOSDN_TEST_PASSWORD")
if p != "" {
testPassword = p
}
}
func TestMain(m *testing.M) {
testSetupIntegration()
os.Exit(m.Run())
}
func TestCliIntegration(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
tests := []struct {
name string
wantErr bool
}{
{
name: "default",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer viper.Reset()
if err := cli.HTTPGet(testApiEndpoint, "init"); (err != nil) != tt.wantErr {
switch err.(type) {
case viper.ConfigFileNotFoundError:
default:
t.Errorf("gosdn cli init error = %v, wantErr %v", err, tt.wantErr)
return
}
}
cliPnd = viper.GetString("CLI_PND")
cliSbi = viper.GetString("CLI_SBI")
testApiEndpoint,
"addDevice",
"address="+testAddress,
"password="+testPassword,
"username="+testUsername,
"sbi="+cliSbi,
"pnd="+cliPnd,
); (err != nil) != tt.wantErr {
t.Errorf("gosdn cli add-device error = %v, wantErr %v", err, tt.wantErr)
return
}
did := viper.GetString("LAST_DEVICE_UUID")
testApiEndpoint,
"request",
"uuid="+did,
"sbi="+cliSbi,
"pnd="+cliPnd,
"path=/system/config/hostname",
); (err != nil) != tt.wantErr {
t.Errorf("gosdn cli request error = %v, wantErr %v", err, tt.wantErr)
return
}
testApiEndpoint,
"getDevice",
"address="+testAddress,
"uuid="+did,
"sbi="+cliSbi,
"pnd="+cliPnd,
); (err != nil) != tt.wantErr {
t.Errorf("gosdn cli get-device error = %v, wantErr %v", err, tt.wantErr)
return
}
hostname := guuid.New().String()
testApiEndpoint,
"set",
"address="+testAddress,
"uuid="+did,
"sbi="+cliSbi,
"pnd="+cliPnd,
"path=/system/config/hostname",
"value="+hostname,
); (err != nil) != tt.wantErr {
t.Errorf("gosdn cli set error = %v, wantErr %v", err, tt.wantErr)
return
}
resp, err := cli.Get(testAddress, testUsername, testPassword, "/system/config/hostname")
if (err != nil) != tt.wantErr {
t.Errorf("cli.Get() error = %v, wantErr %v", err, tt.wantErr)
return
}
got := resp.Notification[0].Update[0].Val.GetStringVal()
if got != hostname {
t.Errorf("integration test failed = got: %v, want: %v", got, hostname)
}
})
}
}