github.imxd.top/operator-framework/operator-sdk@v0.8.2/pkg/ansible/proxy/proxy_test.go (about)

     1  // Copyright 2018 The Operator-SDK Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package proxy
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  	"io/ioutil"
    21  	"net/http"
    22  	"testing"
    23  
    24  	"github.com/operator-framework/operator-sdk/internal/util/fileutil"
    25  	"github.com/operator-framework/operator-sdk/pkg/ansible/proxy/controllermap"
    26  
    27  	kcorev1 "k8s.io/api/core/v1"
    28  	kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/apimachinery/pkg/runtime"
    30  	"k8s.io/client-go/rest"
    31  	"sigs.k8s.io/controller-runtime/pkg/client"
    32  	"sigs.k8s.io/controller-runtime/pkg/client/config"
    33  	"sigs.k8s.io/controller-runtime/pkg/manager"
    34  )
    35  
    36  func TestHandler(t *testing.T) {
    37  	if testing.Short() {
    38  		t.Skip("skipping ansible proxy testing in short mode")
    39  	}
    40  	mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{Namespace: "default"})
    41  	if err != nil {
    42  		t.Fatalf("Failed to instantiate manager: %v", err)
    43  	}
    44  	done := make(chan error)
    45  	cMap := controllermap.NewControllerMap()
    46  	err = Run(done, Options{
    47  		Address:           "localhost",
    48  		Port:              8888,
    49  		KubeConfig:        mgr.GetConfig(),
    50  		Cache:             nil,
    51  		RESTMapper:        mgr.GetRESTMapper(),
    52  		ControllerMap:     cMap,
    53  		WatchedNamespaces: []string{"default"},
    54  	})
    55  	if err != nil {
    56  		t.Fatalf("Error starting proxy: %v", err)
    57  	}
    58  
    59  	po := createPod("test", "default", mgr.GetConfig())
    60  
    61  	resp, err := http.Get("http://localhost:8888/api/v1/namespaces/default/pods/test")
    62  	if err != nil {
    63  		t.Fatalf("Error getting pod from proxy: %v", err)
    64  	}
    65  	defer func() {
    66  		if err := resp.Body.Close(); err != nil && !fileutil.IsClosedError(err) {
    67  			t.Errorf("Failed to close response body: (%v)", err)
    68  		}
    69  	}()
    70  	body, err := ioutil.ReadAll(resp.Body)
    71  	if err != nil {
    72  		t.Fatalf("Error reading response body: %v", err)
    73  	}
    74  	// Should only be one string from 'X-Cache' header (explicitly set to HIT in proxy)
    75  	if resp.Header["X-Cache"] == nil {
    76  		t.Fatalf("Object was not retrieved from cache")
    77  		if resp.Header["X-Cache"][0] != "HIT" {
    78  			t.Fatalf("Cache response header found but got [%v], expected [HIT]", resp.Header["X-Cache"][0])
    79  		}
    80  	}
    81  	data := kcorev1.Pod{}
    82  	err = json.Unmarshal(body, &data)
    83  	if data.Name != "test" {
    84  		t.Fatalf("Got unexpected pod name: %#v", data.Name)
    85  	}
    86  	deletePod(po, mgr.GetConfig())
    87  }
    88  
    89  func createPod(name, namespace string, cfg *rest.Config) runtime.Object {
    90  	three := int64(3)
    91  	pod := &kcorev1.Pod{
    92  		ObjectMeta: kmetav1.ObjectMeta{
    93  			Name:      name,
    94  			Namespace: namespace,
    95  			Labels: map[string]string{
    96  				"test-label": name,
    97  			},
    98  		},
    99  		Spec: kcorev1.PodSpec{
   100  			Containers:            []kcorev1.Container{{Name: "nginx", Image: "nginx"}},
   101  			RestartPolicy:         "Always",
   102  			ActiveDeadlineSeconds: &three,
   103  		},
   104  	}
   105  	cl, err := client.New(cfg, client.Options{})
   106  	err = cl.Create(context.Background(), pod)
   107  	if err != nil {
   108  		return nil
   109  	}
   110  	return pod
   111  }
   112  
   113  func deletePod(pod runtime.Object, cfg *rest.Config) {
   114  	cl, err := client.New(cfg, client.Options{})
   115  	err = cl.Delete(context.Background(), pod)
   116  	if err != nil {
   117  		return
   118  	}
   119  }