github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/pkg/registry/controller_registry_test.go (about)

     1  /*
     2  Copyright 2014 Google Inc. All rights reserved.
     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  package registry
    17  
    18  import (
    19  	"encoding/json"
    20  	"fmt"
    21  	"io/ioutil"
    22  	"reflect"
    23  	"testing"
    24  
    25  	. "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
    26  )
    27  
    28  type MockControllerRegistry struct {
    29  	err         error
    30  	controllers []ReplicationController
    31  }
    32  
    33  func (registry *MockControllerRegistry) ListControllers() ([]ReplicationController, error) {
    34  	return registry.controllers, registry.err
    35  }
    36  
    37  func (registry *MockControllerRegistry) GetController(ID string) (*ReplicationController, error) {
    38  	return &ReplicationController{}, registry.err
    39  }
    40  
    41  func (registry *MockControllerRegistry) CreateController(controller ReplicationController) error {
    42  	return registry.err
    43  }
    44  
    45  func (registry *MockControllerRegistry) UpdateController(controller ReplicationController) error {
    46  	return registry.err
    47  }
    48  func (registry *MockControllerRegistry) DeleteController(ID string) error {
    49  	return registry.err
    50  }
    51  
    52  func TestListControllersError(t *testing.T) {
    53  	mockRegistry := MockControllerRegistry{
    54  		err: fmt.Errorf("Test Error"),
    55  	}
    56  	storage := ControllerRegistryStorage{
    57  		registry: &mockRegistry,
    58  	}
    59  	controllersObj, err := storage.List(nil)
    60  	controllers := controllersObj.(ReplicationControllerList)
    61  	if err != mockRegistry.err {
    62  		t.Errorf("Expected %#v, Got %#v", mockRegistry.err, err)
    63  	}
    64  	if len(controllers.Items) != 0 {
    65  		t.Errorf("Unexpected non-zero task list: %#v", controllers)
    66  	}
    67  }
    68  
    69  func TestListEmptyControllerList(t *testing.T) {
    70  	mockRegistry := MockControllerRegistry{}
    71  	storage := ControllerRegistryStorage{
    72  		registry: &mockRegistry,
    73  	}
    74  	controllers, err := storage.List(nil)
    75  	expectNoError(t, err)
    76  	if len(controllers.(ReplicationControllerList).Items) != 0 {
    77  		t.Errorf("Unexpected non-zero task list: %#v", controllers)
    78  	}
    79  }
    80  
    81  func TestListControllerList(t *testing.T) {
    82  	mockRegistry := MockControllerRegistry{
    83  		controllers: []ReplicationController{
    84  			ReplicationController{
    85  				JSONBase: JSONBase{
    86  					ID: "foo",
    87  				},
    88  			},
    89  			ReplicationController{
    90  				JSONBase: JSONBase{
    91  					ID: "bar",
    92  				},
    93  			},
    94  		},
    95  	}
    96  	storage := ControllerRegistryStorage{
    97  		registry: &mockRegistry,
    98  	}
    99  	controllersObj, err := storage.List(nil)
   100  	controllers := controllersObj.(ReplicationControllerList)
   101  	expectNoError(t, err)
   102  	if len(controllers.Items) != 2 {
   103  		t.Errorf("Unexpected controller list: %#v", controllers)
   104  	}
   105  	if controllers.Items[0].ID != "foo" {
   106  		t.Errorf("Unexpected controller: %#v", controllers.Items[0])
   107  	}
   108  	if controllers.Items[1].ID != "bar" {
   109  		t.Errorf("Unexpected controller: %#v", controllers.Items[1])
   110  	}
   111  }
   112  
   113  func TestExtractControllerJson(t *testing.T) {
   114  	mockRegistry := MockControllerRegistry{}
   115  	storage := ControllerRegistryStorage{
   116  		registry: &mockRegistry,
   117  	}
   118  	controller := ReplicationController{
   119  		JSONBase: JSONBase{
   120  			ID: "foo",
   121  		},
   122  	}
   123  	body, err := json.Marshal(controller)
   124  	expectNoError(t, err)
   125  	controllerOut, err := storage.Extract(string(body))
   126  	expectNoError(t, err)
   127  	jsonOut, err := json.Marshal(controllerOut)
   128  	expectNoError(t, err)
   129  	if string(body) != string(jsonOut) {
   130  		t.Errorf("Expected %#v, found %#v", controller, controllerOut)
   131  	}
   132  }
   133  
   134  func TestControllerParsing(t *testing.T) {
   135  	expectedController := ReplicationController{
   136  		JSONBase: JSONBase{
   137  			ID: "nginxController",
   138  		},
   139  		DesiredState: ReplicationControllerState{
   140  			Replicas: 2,
   141  			ReplicasInSet: map[string]string{
   142  				"name": "nginx",
   143  			},
   144  			TaskTemplate: TaskTemplate{
   145  				DesiredState: TaskState{
   146  					Manifest: ContainerManifest{
   147  						Containers: []Container{
   148  							Container{
   149  								Image: "dockerfile/nginx",
   150  								Ports: []Port{
   151  									Port{
   152  										ContainerPort: 80,
   153  										HostPort:      8080,
   154  									},
   155  								},
   156  							},
   157  						},
   158  					},
   159  				},
   160  				Labels: map[string]string{
   161  					"name": "nginx",
   162  				},
   163  			},
   164  		},
   165  		Labels: map[string]string{
   166  			"name": "nginx",
   167  		},
   168  	}
   169  	file, err := ioutil.TempFile("", "controller")
   170  	fileName := file.Name()
   171  	expectNoError(t, err)
   172  	data, err := json.Marshal(expectedController)
   173  	expectNoError(t, err)
   174  	_, err = file.Write(data)
   175  	expectNoError(t, err)
   176  	err = file.Close()
   177  	expectNoError(t, err)
   178  	data, err = ioutil.ReadFile(fileName)
   179  	expectNoError(t, err)
   180  	var controller ReplicationController
   181  	err = json.Unmarshal(data, &controller)
   182  	expectNoError(t, err)
   183  
   184  	if !reflect.DeepEqual(controller, expectedController) {
   185  		t.Errorf("Parsing failed: %s %#v %#v", string(data), controller, expectedController)
   186  	}
   187  }