2020-02-11 23:27:43 +00:00
|
|
|
package nodeconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2023-01-26 00:43:57 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/config"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2020-02-11 23:27:43 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var FakeNodeWithNoAnnotation = &corev1.Node{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "Node",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "fakeNode-no-annotation",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:44:11 +00:00
|
|
|
var TestEnvName = version.ProgramUpper + "_NODE_NAME"
|
2023-01-26 00:43:57 +00:00
|
|
|
var FakeNodeConfig = &config.Node{}
|
2020-02-11 23:27:43 +00:00
|
|
|
var FakeNodeWithAnnotation = &corev1.Node{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "Node",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "fakeNode-with-annotation",
|
|
|
|
Annotations: map[string]string{
|
2022-12-05 22:01:01 +00:00
|
|
|
NodeArgsAnnotation: `["server","--flannel-backend=none"]`,
|
2021-07-13 17:44:11 +00:00
|
|
|
NodeEnvAnnotation: `{"` + TestEnvName + `":"fakeNode-with-annotation"}`,
|
2022-12-05 22:01:01 +00:00
|
|
|
NodeConfigHashAnnotation: "5E6GSWFRVCOEB3BFFVXKWVD7IQEVJFJAALHPOTCLV7SL33N6SIYA====",
|
2020-02-11 23:27:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:59:33 +00:00
|
|
|
func Test_UnitSetExistingNodeConfigAnnotations(t *testing.T) {
|
2020-02-11 23:27:43 +00:00
|
|
|
// adding same config
|
2022-12-05 22:01:01 +00:00
|
|
|
os.Args = []string{version.Program, "server", "--flannel-backend=none"}
|
2020-05-05 22:09:04 +00:00
|
|
|
os.Setenv(version.ProgramUpper+"_NODE_NAME", "fakeNode-with-annotation")
|
2023-01-26 00:43:57 +00:00
|
|
|
nodeUpdated, err := SetNodeConfigAnnotations(FakeNodeConfig, FakeNodeWithAnnotation)
|
2020-02-11 23:27:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to set node config annotation: %v", err)
|
|
|
|
}
|
2021-07-13 17:44:11 +00:00
|
|
|
if nodeUpdated {
|
2021-07-26 16:59:33 +00:00
|
|
|
t.Errorf("Test_UnitSetExistingNodeConfigAnnotations() expected false")
|
2021-07-13 17:44:11 +00:00
|
|
|
}
|
2020-02-11 23:27:43 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 16:59:33 +00:00
|
|
|
func Test_UnitSetNodeConfigAnnotations(t *testing.T) {
|
2021-07-13 17:44:11 +00:00
|
|
|
type args struct {
|
2023-01-26 00:43:57 +00:00
|
|
|
config *config.Node
|
2021-07-13 17:44:11 +00:00
|
|
|
node *corev1.Node
|
|
|
|
osArgs []string
|
|
|
|
}
|
|
|
|
setup := func(osArgs []string) error {
|
|
|
|
os.Args = osArgs
|
|
|
|
return os.Setenv(TestEnvName, "fakeNode-with-no-annotation")
|
|
|
|
}
|
|
|
|
teardown := func() error {
|
|
|
|
return os.Unsetenv(TestEnvName)
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want bool
|
|
|
|
wantErr bool
|
|
|
|
wantNodeArgs string
|
|
|
|
wantNodeEnv string
|
|
|
|
wantNodeConfigHash string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Set empty NodeConfigAnnotations",
|
|
|
|
args: args{
|
2023-01-26 00:43:57 +00:00
|
|
|
config: FakeNodeConfig,
|
2021-07-13 17:44:11 +00:00
|
|
|
node: FakeNodeWithAnnotation,
|
2022-12-05 22:01:01 +00:00
|
|
|
osArgs: []string{version.Program, "server", "--flannel-backend=none"},
|
2021-07-13 17:44:11 +00:00
|
|
|
},
|
|
|
|
want: true,
|
2022-12-05 22:01:01 +00:00
|
|
|
wantNodeArgs: `["server","--flannel-backend","none"]`,
|
2021-07-13 17:44:11 +00:00
|
|
|
wantNodeEnv: `{"` + TestEnvName + `":"fakeNode-with-no-annotation"}`,
|
2022-12-05 22:01:01 +00:00
|
|
|
wantNodeConfigHash: "DRWW63TXZZGSKLARSFZLNSJ3RZ6VR7LQ46WPKZMSLTSGNI2J42WA====",
|
2021-07-13 17:44:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Set args with equal",
|
|
|
|
args: args{
|
2023-01-26 00:43:57 +00:00
|
|
|
config: FakeNodeConfig,
|
2021-07-13 17:44:11 +00:00
|
|
|
node: FakeNodeWithNoAnnotation,
|
2022-12-05 22:01:01 +00:00
|
|
|
osArgs: []string{version.Program, "server", "--flannel-backend=none", "--write-kubeconfig-mode=777"},
|
2021-07-13 17:44:11 +00:00
|
|
|
},
|
|
|
|
want: true,
|
2022-12-05 22:01:01 +00:00
|
|
|
wantNodeArgs: `["server","--flannel-backend","none","--write-kubeconfig-mode","777"]`,
|
2021-07-13 17:44:11 +00:00
|
|
|
wantNodeEnv: `{"` + TestEnvName + `":"fakeNode-with-no-annotation"}`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
defer teardown()
|
|
|
|
if err := setup(tt.args.osArgs); err != nil {
|
|
|
|
t.Errorf("Setup for SetNodeConfigAnnotations() failed = %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-26 00:43:57 +00:00
|
|
|
got, err := SetNodeConfigAnnotations(tt.args.config, tt.args.node)
|
2021-07-13 17:44:11 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("SetNodeConfigAnnotations() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("SetNodeConfigAnnotations() = %+v\nWantRes = %+v", got, tt.want)
|
|
|
|
}
|
|
|
|
nodeAnn := tt.args.node.Annotations
|
|
|
|
if nodeAnn[NodeArgsAnnotation] != tt.wantNodeArgs {
|
|
|
|
t.Errorf("SetNodeConfigAnnotations() = %+v\nWantAnn.nodeArgs = %+v", nodeAnn[NodeArgsAnnotation], tt.wantNodeArgs)
|
|
|
|
}
|
|
|
|
if nodeAnn[NodeEnvAnnotation] != tt.wantNodeEnv {
|
|
|
|
t.Errorf("SetNodeConfigAnnotations() = %+v\nWantAnn.nodeEnv = %+v", nodeAnn[NodeEnvAnnotation], tt.wantNodeEnv)
|
|
|
|
}
|
|
|
|
if tt.wantNodeConfigHash != "" && nodeAnn[NodeConfigHashAnnotation] != tt.wantNodeConfigHash {
|
|
|
|
t.Errorf("SetNodeConfigAnnotations() = %+v\nWantAnn.nodeConfigHash = %+v", nodeAnn[NodeConfigHashAnnotation], tt.wantNodeConfigHash)
|
|
|
|
}
|
|
|
|
})
|
2020-02-11 23:27:43 +00:00
|
|
|
}
|
|
|
|
}
|