...

Source file src/k8s.io/kubernetes/cmd/kubemark/app/hollow_node_test.go

Documentation: k8s.io/kubernetes/cmd/kubemark/app

     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  
    29  const fakeKubeconfig = `
    30  apiVersion: v1
    31  kind: Config
    32  clusters:
    33  - cluster:
    34      insecure-skip-tls-verify: true
    35      server: %s
    36    name: default
    37  contexts:
    38  - context:
    39      cluster: default
    40      user: default
    41    name: default
    42  current-context: default
    43  users:
    44  - name: default
    45    user:
    46      username: config
    47  `
    48  
    49  // TestHollowNode is a naive test that attempts to start hollow node and checks if it's not crashing.
    50  // Such test is sufficient to detect e.g. missing kubelet dependencies that are not added in
    51  // pkg/kubemark/hollow_kubelet.go.
    52  func TestHollowNode(t *testing.T) {
    53  	// temp dir
    54  	tmpDir, err := os.MkdirTemp("", "hollow-node")
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	defer os.RemoveAll(tmpDir)
    59  
    60  	// https server
    61  	server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    62  		w.WriteHeader(200)
    63  		w.Write([]byte(`ok`))
    64  	}))
    65  	defer server.Close()
    66  
    67  	kubeconfigPath := filepath.Join(tmpDir, "config.kubeconfig")
    68  	if err := os.WriteFile(kubeconfigPath, []byte(fmt.Sprintf(fakeKubeconfig, server.URL)), os.FileMode(0600)); err != nil {
    69  		t.Fatal(err)
    70  	}
    71  
    72  	for morph := range knownMorphs {
    73  		morph := morph
    74  		t.Run(morph, func(t *testing.T) {
    75  			s := &hollowNodeConfig{
    76  				KubeconfigPath: kubeconfigPath,
    77  				Morph:          morph,
    78  			}
    79  			errCh := make(chan error)
    80  			go func() {
    81  				data, err := os.ReadFile(kubeconfigPath)
    82  				t.Logf("read %d, err=%v\n", len(data), err)
    83  				errCh <- run(s)
    84  			}()
    85  
    86  			select {
    87  			case err := <-errCh:
    88  				t.Fatalf("Run finished unexpectedly with error: %v", err)
    89  			case <-time.After(3 * time.Second):
    90  				t.Logf("Morph %q hasn't crashed for 3s. Calling success.", morph)
    91  			}
    92  		})
    93  	}
    94  }
    95  

View as plain text