github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/pkg/registry/memory_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  	"testing"
    20  
    21  	. "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
    22  )
    23  
    24  func TestListTasksEmpty(t *testing.T) {
    25  	registry := MakeMemoryRegistry()
    26  	tasks, err := registry.ListTasks(nil)
    27  	expectNoError(t, err)
    28  	if len(tasks) != 0 {
    29  		t.Errorf("Unexpected task list: %#v", tasks)
    30  	}
    31  }
    32  
    33  func TestMemoryListTasks(t *testing.T) {
    34  	registry := MakeMemoryRegistry()
    35  	registry.CreateTask("machine", Task{JSONBase: JSONBase{ID: "foo"}})
    36  	tasks, err := registry.ListTasks(nil)
    37  	expectNoError(t, err)
    38  	if len(tasks) != 1 || tasks[0].ID != "foo" {
    39  		t.Errorf("Unexpected task list: %#v", tasks)
    40  	}
    41  }
    42  
    43  func TestMemorySetGetTasks(t *testing.T) {
    44  	registry := MakeMemoryRegistry()
    45  	expectedTask := Task{JSONBase: JSONBase{ID: "foo"}}
    46  	registry.CreateTask("machine", expectedTask)
    47  	task, err := registry.GetTask("foo")
    48  	expectNoError(t, err)
    49  	if expectedTask.ID != task.ID {
    50  		t.Errorf("Unexpected task, expected %#v, actual %#v", expectedTask, task)
    51  	}
    52  }
    53  
    54  func TestMemorySetUpdateGetTasks(t *testing.T) {
    55  	registry := MakeMemoryRegistry()
    56  	oldTask := Task{JSONBase: JSONBase{ID: "foo"}}
    57  	expectedTask := Task{
    58  		JSONBase: JSONBase{
    59  			ID: "foo",
    60  		},
    61  		DesiredState: TaskState{
    62  			Host: "foo.com",
    63  		},
    64  	}
    65  	registry.CreateTask("machine", oldTask)
    66  	registry.UpdateTask(expectedTask)
    67  	task, err := registry.GetTask("foo")
    68  	expectNoError(t, err)
    69  	if expectedTask.ID != task.ID || task.DesiredState.Host != expectedTask.DesiredState.Host {
    70  		t.Errorf("Unexpected task, expected %#v, actual %#v", expectedTask, task)
    71  	}
    72  }
    73  
    74  func TestMemorySetDeleteGetTasks(t *testing.T) {
    75  	registry := MakeMemoryRegistry()
    76  	expectedTask := Task{JSONBase: JSONBase{ID: "foo"}}
    77  	registry.CreateTask("machine", expectedTask)
    78  	registry.DeleteTask("foo")
    79  	task, err := registry.GetTask("foo")
    80  	expectNoError(t, err)
    81  	if task != nil {
    82  		t.Errorf("Unexpected task: %#v", task)
    83  	}
    84  }
    85  
    86  func TestListControllersEmpty(t *testing.T) {
    87  	registry := MakeMemoryRegistry()
    88  	tasks, err := registry.ListControllers()
    89  	expectNoError(t, err)
    90  	if len(tasks) != 0 {
    91  		t.Errorf("Unexpected task list: %#v", tasks)
    92  	}
    93  }
    94  
    95  func TestMemoryListControllers(t *testing.T) {
    96  	registry := MakeMemoryRegistry()
    97  	registry.CreateController(ReplicationController{JSONBase: JSONBase{ID: "foo"}})
    98  	tasks, err := registry.ListControllers()
    99  	expectNoError(t, err)
   100  	if len(tasks) != 1 || tasks[0].ID != "foo" {
   101  		t.Errorf("Unexpected task list: %#v", tasks)
   102  	}
   103  }
   104  
   105  func TestMemorySetGetControllers(t *testing.T) {
   106  	registry := MakeMemoryRegistry()
   107  	expectedController := ReplicationController{JSONBase: JSONBase{ID: "foo"}}
   108  	registry.CreateController(expectedController)
   109  	task, err := registry.GetController("foo")
   110  	expectNoError(t, err)
   111  	if expectedController.ID != task.ID {
   112  		t.Errorf("Unexpected task, expected %#v, actual %#v", expectedController, task)
   113  	}
   114  }
   115  
   116  func TestMemorySetUpdateGetControllers(t *testing.T) {
   117  	registry := MakeMemoryRegistry()
   118  	oldController := ReplicationController{JSONBase: JSONBase{ID: "foo"}}
   119  	expectedController := ReplicationController{
   120  		JSONBase: JSONBase{
   121  			ID: "foo",
   122  		},
   123  		DesiredState: ReplicationControllerState{
   124  			Replicas: 2,
   125  		},
   126  	}
   127  	registry.CreateController(oldController)
   128  	registry.UpdateController(expectedController)
   129  	task, err := registry.GetController("foo")
   130  	expectNoError(t, err)
   131  	if expectedController.ID != task.ID || task.DesiredState.Replicas != expectedController.DesiredState.Replicas {
   132  		t.Errorf("Unexpected task, expected %#v, actual %#v", expectedController, task)
   133  	}
   134  }
   135  
   136  func TestMemorySetDeleteGetControllers(t *testing.T) {
   137  	registry := MakeMemoryRegistry()
   138  	expectedController := ReplicationController{JSONBase: JSONBase{ID: "foo"}}
   139  	registry.CreateController(expectedController)
   140  	registry.DeleteController("foo")
   141  	task, err := registry.GetController("foo")
   142  	expectNoError(t, err)
   143  	if task != nil {
   144  		t.Errorf("Unexpected task: %#v", task)
   145  	}
   146  }