Skip to content

Commit

Permalink
some tests for config data
Browse files Browse the repository at this point in the history
  • Loading branch information
enrichman committed Feb 4, 2025
1 parent 7234729 commit 4f856b8
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 7 deletions.
114 changes: 114 additions & 0 deletions pkg/controller/cluster/agent/shared_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package agent

import (
"testing"

"github.com/rancher/k3k/pkg/apis/k3k.io/v1alpha1"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Test_sharedAgentData(t *testing.T) {
type args struct {
cluster *v1alpha1.Cluster
serviceName string
ip string
token string
}
tests := []struct {
name string
args args
expectedData map[string]string
}{
{
name: "simple config",
args: args{
cluster: &v1alpha1.Cluster{
ObjectMeta: v1.ObjectMeta{
Name: "mycluster",
Namespace: "ns-1",
},
Spec: v1alpha1.ClusterSpec{
Version: "v1.2.3",
},
},
ip: "10.0.0.21",
serviceName: "service-name",
token: "dnjklsdjnksd892389238",
},
expectedData: map[string]string{
"clusterName": "mycluster",
"clusterNamespace": "ns-1",
"serverIP": "10.0.0.21",
"serviceName": "service-name",
"token": "dnjklsdjnksd892389238",
"version": "v1.2.3",
},
},
{
name: "version in status",
args: args{
cluster: &v1alpha1.Cluster{
ObjectMeta: v1.ObjectMeta{
Name: "mycluster",
Namespace: "ns-1",
},
Spec: v1alpha1.ClusterSpec{
Version: "v1.2.3",
},
Status: v1alpha1.ClusterStatus{
HostVersion: "v1.3.3",
},
},
ip: "10.0.0.21",
serviceName: "service-name",
token: "dnjklsdjnksd892389238",
},
expectedData: map[string]string{
"clusterName": "mycluster",
"clusterNamespace": "ns-1",
"serverIP": "10.0.0.21",
"serviceName": "service-name",
"token": "dnjklsdjnksd892389238",
"version": "v1.2.3",
},
},
{
name: "missing version in spec",
args: args{
cluster: &v1alpha1.Cluster{
ObjectMeta: v1.ObjectMeta{
Name: "mycluster",
Namespace: "ns-1",
},
Status: v1alpha1.ClusterStatus{
HostVersion: "v1.3.3",
},
},
ip: "10.0.0.21",
serviceName: "service-name",
token: "dnjklsdjnksd892389238",
},
expectedData: map[string]string{
"clusterName": "mycluster",
"clusterNamespace": "ns-1",
"serverIP": "10.0.0.21",
"serviceName": "service-name",
"token": "dnjklsdjnksd892389238",
"version": "v1.3.3",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := sharedAgentData(tt.args.cluster, tt.args.serviceName, tt.args.token, tt.args.ip)

data := make(map[string]string)
err := yaml.Unmarshal([]byte(config), data)

assert.NoError(t, err)
assert.Equal(t, tt.expectedData, data)
})
}
}
19 changes: 12 additions & 7 deletions pkg/controller/cluster/agent/virtual_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package agent

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -14,16 +13,21 @@ func Test_virtualAgentData(t *testing.T) {
token string
}
tests := []struct {
name string
args args
want string
name string
args args
expectedData map[string]string
}{
{
name: "test",
name: "simple config",
args: args{
serviceIP: "10.0.0.21",
token: "dnjklsdjnksd892389238",
},
expectedData: map[string]string{
"server": "https://10.0.0.21:6443",
"token": "dnjklsdjnksd892389238",
"with-node-id": "true",
},
want: "server: https://%s:6443",
},
}
for _, tt := range tests {
Expand All @@ -32,8 +36,9 @@ func Test_virtualAgentData(t *testing.T) {

data := make(map[string]string)
err := yaml.Unmarshal([]byte(config), data)

assert.NoError(t, err)
fmt.Println(data)
assert.Equal(t, tt.expectedData, data)
})
}
}

0 comments on commit 4f856b8

Please sign in to comment.