1
1
package e2e_test
2
2
3
3
import (
4
- "testing "
4
+ "fmt "
5
5
"strings"
6
+ "testing"
7
+ "time"
6
8
9
+ ignv2_2types "github.com/coreos/ignition/config/v2_2/types"
7
10
"k8s.io/api/core/v1"
8
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9
12
"k8s.io/apimachinery/pkg/labels"
13
+ "k8s.io/apimachinery/pkg/util/uuid"
14
+ "k8s.io/apimachinery/pkg/util/wait"
10
15
11
16
"github.com/openshift/machine-config-operator/cmd/common"
17
+ mcv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
12
18
)
13
19
14
20
// Test case for https://github.com/openshift/machine-config-operator/issues/358
15
21
func TestMCDToken (t * testing.T ) {
16
22
cb , err := common .NewClientBuilder ("" )
17
- if err != nil {
23
+ if err != nil {
18
24
t .Errorf ("%#v" , err )
19
25
}
20
- k := cb .KubeClientOrDie ("sanity -test" )
26
+ k := cb .KubeClientOrDie ("mcd-token -test" )
21
27
22
28
listOptions := metav1.ListOptions {
23
29
LabelSelector : labels .SelectorFromSet (labels.Set {"k8s-app" : "machine-config-daemon" }).String (),
@@ -40,3 +46,103 @@ func TestMCDToken(t *testing.T) {
40
46
}
41
47
}
42
48
}
49
+
50
+ func mcLabelForWorkers () map [string ]string {
51
+ mcLabels := make (map [string ]string )
52
+ mcLabels ["machineconfiguration.openshift.io/role" ] = "worker"
53
+ return mcLabels
54
+ }
55
+
56
+ func createMCFile (path , content string , mode int ) ignv2_2types.File {
57
+ return ignv2_2types.File {
58
+ FileEmbedded1 : ignv2_2types.FileEmbedded1 {
59
+ Contents : ignv2_2types.FileContents {
60
+ Source : content ,
61
+ },
62
+ Mode : & mode ,
63
+ },
64
+ Node : ignv2_2types.Node {
65
+ Filesystem : "root" ,
66
+ Path : path ,
67
+ },
68
+ }
69
+ }
70
+
71
+ func TestMCDeployed (t * testing.T ) {
72
+ cb , err := common .NewClientBuilder ("" )
73
+ if err != nil {
74
+ t .Errorf ("%#v" , err )
75
+ }
76
+ mcClient := cb .MachineConfigClientOrDie ("mc-file-add" )
77
+ k := cb .KubeClientOrDie ("mc-file-add" )
78
+
79
+ // create a dummy MC
80
+ mcName := fmt .Sprintf ("00-0add-a-file-%s" , uuid .NewUUID ())
81
+ mcadd := & mcv1.MachineConfig {}
82
+ mcadd .ObjectMeta = metav1.ObjectMeta {
83
+ Name : mcName ,
84
+ Labels : mcLabelForWorkers (),
85
+ }
86
+ mcadd .Spec = mcv1.MachineConfigSpec {
87
+ Config : ignv2_2types.Config {
88
+ Ignition : ignv2_2types.Ignition {
89
+ Version : "2.2.0" ,
90
+ },
91
+ Storage : ignv2_2types.Storage {
92
+ Files : []ignv2_2types.File {
93
+ createMCFile ("/etc/mytestconf" , "data:,test" , 420 ),
94
+ },
95
+ },
96
+ },
97
+ }
98
+
99
+ // create the dummy MC now
100
+ _ , err = mcClient .MachineconfigurationV1 ().MachineConfigs ().Create (mcadd )
101
+ if err != nil {
102
+ t .Errorf ("failed to create machine config %v" , err )
103
+ }
104
+
105
+ // grab the latest worker- MC
106
+ var newMCName string
107
+ err = wait .Poll (2 * time .Second , 5 * time .Minute , func () (bool , error ) {
108
+ mcp , err := mcClient .MachineconfigurationV1 ().MachineConfigPools ().Get ("worker" , metav1.GetOptions {})
109
+ if err != nil {
110
+ return false , err
111
+ }
112
+ for _ , mc := range mcp .Status .Configuration .Source {
113
+ if mc .Name == mcName {
114
+ newMCName = mcp .Status .Configuration .Name
115
+ return true , nil
116
+ }
117
+ }
118
+ return false , nil
119
+ })
120
+
121
+ listOptions := metav1.ListOptions {
122
+ LabelSelector : labels .SelectorFromSet (labels.Set {"k8s-app" : "machine-config-daemon" }).String (),
123
+ }
124
+
125
+ err = wait .Poll (3 * time .Second , 5 * time .Minute , func () (bool , error ) {
126
+ mcdList , err := k .CoreV1 ().Pods ("openshift-machine-config-operator" ).List (listOptions )
127
+ if err != nil {
128
+ return false , err
129
+ }
130
+
131
+ for _ , pod := range mcdList .Items {
132
+ res , err := k .CoreV1 ().Pods (pod .Namespace ).GetLogs (pod .Name , & v1.PodLogOptions {}).DoRaw ()
133
+ if err != nil {
134
+ // do not error out, we may be rebooting, that's why we list at every iteration
135
+ return false , nil
136
+ }
137
+ for _ , line := range strings .Split (string (res ), "\n " ) {
138
+ if strings .Contains (line , "completed update for config " + newMCName ) {
139
+ return true , nil
140
+ }
141
+ }
142
+ }
143
+ return false , nil
144
+ })
145
+ if err != nil {
146
+ t .Errorf ("machine config didn't result in file being on any worker: %v" , err )
147
+ }
148
+ }
0 commit comments