k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubemark/app/hollow_node_test.go (about) 1 /* 2 Copyright 2023 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package app 18 19 import ( 20 "fmt" 21 "net/http" 22 "net/http/httptest" 23 "os" 24 "path/filepath" 25 "testing" 26 "time" 27 28 "k8s.io/kubernetes/test/utils/ktesting" 29 ) 30 31 const fakeKubeconfig = ` 32 apiVersion: v1 33 kind: Config 34 clusters: 35 - cluster: 36 insecure-skip-tls-verify: true 37 server: %s 38 name: default 39 contexts: 40 - context: 41 cluster: default 42 user: default 43 name: default 44 current-context: default 45 users: 46 - name: default 47 user: 48 username: config 49 ` 50 51 // TestHollowNode is a naive test that attempts to start hollow node and checks if it's not crashing. 52 // Such test is sufficient to detect e.g. missing kubelet dependencies that are not added in 53 // pkg/kubemark/hollow_kubelet.go. 54 func TestHollowNode(t *testing.T) { 55 // temp dir 56 tmpDir, err := os.MkdirTemp("", "hollow-node") 57 if err != nil { 58 t.Fatal(err) 59 } 60 defer os.RemoveAll(tmpDir) 61 62 // https server 63 server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 64 w.WriteHeader(200) 65 w.Write([]byte(`ok`)) 66 })) 67 defer server.Close() 68 69 kubeconfigPath := filepath.Join(tmpDir, "config.kubeconfig") 70 if err := os.WriteFile(kubeconfigPath, []byte(fmt.Sprintf(fakeKubeconfig, server.URL)), os.FileMode(0600)); err != nil { 71 t.Fatal(err) 72 } 73 74 for morph := range knownMorphs { 75 morph := morph 76 t.Run(morph, func(t *testing.T) { 77 s := &hollowNodeConfig{ 78 KubeconfigPath: kubeconfigPath, 79 Morph: morph, 80 } 81 errCh := make(chan error) 82 go func() { 83 data, err := os.ReadFile(kubeconfigPath) 84 t.Logf("read %d, err=%v\n", len(data), err) 85 ctx := ktesting.Init(t) 86 errCh <- run(ctx, s) 87 }() 88 89 select { 90 case err := <-errCh: 91 t.Fatalf("Run finished unexpectedly with error: %v", err) 92 case <-time.After(3 * time.Second): 93 t.Logf("Morph %q hasn't crashed for 3s. Calling success.", morph) 94 } 95 }) 96 } 97 }