github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/pkg/registry/task_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  	"testing"
    22  
    23  	. "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
    24  )
    25  
    26  type MockTaskRegistry struct {
    27  	err   error
    28  	tasks []Task
    29  }
    30  
    31  func expectNoError(t *testing.T, err error) {
    32  	if err != nil {
    33  		t.Errorf("Unexpected error: %#v", err)
    34  	}
    35  }
    36  
    37  func (registry *MockTaskRegistry) ListTasks(*map[string]string) ([]Task, error) {
    38  	return registry.tasks, registry.err
    39  }
    40  
    41  func (registry *MockTaskRegistry) GetTask(taskId string) (*Task, error) {
    42  	return &Task{}, registry.err
    43  }
    44  
    45  func (registry *MockTaskRegistry) CreateTask(machine string, task Task) error {
    46  	return registry.err
    47  }
    48  
    49  func (registry *MockTaskRegistry) UpdateTask(task Task) error {
    50  	return registry.err
    51  }
    52  func (registry *MockTaskRegistry) DeleteTask(taskId string) error {
    53  	return registry.err
    54  }
    55  
    56  func TestListTasksError(t *testing.T) {
    57  	mockRegistry := MockTaskRegistry{
    58  		err: fmt.Errorf("Test Error"),
    59  	}
    60  	storage := TaskRegistryStorage{
    61  		registry: &mockRegistry,
    62  	}
    63  	tasks, err := storage.List(nil)
    64  	if err != mockRegistry.err {
    65  		t.Errorf("Expected %#v, Got %#v", mockRegistry.err, err)
    66  	}
    67  	if len(tasks.(TaskList).Items) != 0 {
    68  		t.Errorf("Unexpected non-zero task list: %#v", tasks)
    69  	}
    70  }
    71  
    72  func TestListEmptyTaskList(t *testing.T) {
    73  	mockRegistry := MockTaskRegistry{}
    74  	storage := TaskRegistryStorage{
    75  		registry: &mockRegistry,
    76  	}
    77  	tasks, err := storage.List(nil)
    78  	expectNoError(t, err)
    79  	if len(tasks.(TaskList).Items) != 0 {
    80  		t.Errorf("Unexpected non-zero task list: %#v", tasks)
    81  	}
    82  }
    83  
    84  func TestListTaskList(t *testing.T) {
    85  	mockRegistry := MockTaskRegistry{
    86  		tasks: []Task{
    87  			Task{
    88  				JSONBase: JSONBase{
    89  					ID: "foo",
    90  				},
    91  			},
    92  			Task{
    93  				JSONBase: JSONBase{
    94  					ID: "bar",
    95  				},
    96  			},
    97  		},
    98  	}
    99  	storage := TaskRegistryStorage{
   100  		registry: &mockRegistry,
   101  	}
   102  	tasksObj, err := storage.List(nil)
   103  	tasks := tasksObj.(TaskList)
   104  	expectNoError(t, err)
   105  	if len(tasks.Items) != 2 {
   106  		t.Errorf("Unexpected task list: %#v", tasks)
   107  	}
   108  	if tasks.Items[0].ID != "foo" {
   109  		t.Errorf("Unexpected task: %#v", tasks.Items[0])
   110  	}
   111  	if tasks.Items[1].ID != "bar" {
   112  		t.Errorf("Unexpected task: %#v", tasks.Items[1])
   113  	}
   114  }
   115  
   116  func TestExtractJson(t *testing.T) {
   117  	mockRegistry := MockTaskRegistry{}
   118  	storage := TaskRegistryStorage{
   119  		registry: &mockRegistry,
   120  	}
   121  	task := Task{
   122  		JSONBase: JSONBase{
   123  			ID: "foo",
   124  		},
   125  	}
   126  	body, err := json.Marshal(task)
   127  	expectNoError(t, err)
   128  	taskOut, err := storage.Extract(string(body))
   129  	expectNoError(t, err)
   130  	jsonOut, err := json.Marshal(taskOut)
   131  	expectNoError(t, err)
   132  	if string(body) != string(jsonOut) {
   133  		t.Errorf("Expected %#v, found %#v", task, taskOut)
   134  	}
   135  }
   136  
   137  func expectLabelMatch(t *testing.T, task Task, key, value string) {
   138  	if !LabelMatch(task, key, value) {
   139  		t.Errorf("Unexpected match failure: %#v %s %s", task, key, value)
   140  	}
   141  }
   142  
   143  func expectNoLabelMatch(t *testing.T, task Task, key, value string) {
   144  	if LabelMatch(task, key, value) {
   145  		t.Errorf("Unexpected match success: %#v %s %s", task, key, value)
   146  	}
   147  }
   148  
   149  func expectLabelsMatch(t *testing.T, task Task, query *map[string]string) {
   150  	if !LabelsMatch(task, query) {
   151  		t.Errorf("Unexpected match failure: %#v %#v", task, *query)
   152  	}
   153  }
   154  
   155  func expectNoLabelsMatch(t *testing.T, task Task, query *map[string]string) {
   156  	if LabelsMatch(task, query) {
   157  		t.Errorf("Unexpected match success: %#v %#v", task, *query)
   158  	}
   159  }
   160  
   161  func TestLabelMatch(t *testing.T) {
   162  	task := Task{
   163  		Labels: map[string]string{
   164  			"foo": "bar",
   165  			"baz": "blah",
   166  		},
   167  	}
   168  	expectLabelMatch(t, task, "foo", "bar")
   169  	expectLabelMatch(t, task, "baz", "blah")
   170  	expectNoLabelMatch(t, task, "foo", "blah")
   171  	expectNoLabelMatch(t, task, "baz", "bar")
   172  }
   173  
   174  func TestLabelsMatch(t *testing.T) {
   175  	task := Task{
   176  		Labels: map[string]string{
   177  			"foo": "bar",
   178  			"baz": "blah",
   179  		},
   180  	}
   181  	expectLabelsMatch(t, task, &map[string]string{})
   182  	expectLabelsMatch(t, task, &map[string]string{
   183  		"foo": "bar",
   184  	})
   185  	expectLabelsMatch(t, task, &map[string]string{
   186  		"baz": "blah",
   187  	})
   188  	expectLabelsMatch(t, task, &map[string]string{
   189  		"foo": "bar",
   190  		"baz": "blah",
   191  	})
   192  	expectNoLabelsMatch(t, task, &map[string]string{
   193  		"foo": "blah",
   194  	})
   195  	expectNoLabelsMatch(t, task, &map[string]string{
   196  		"baz": "bar",
   197  	})
   198  	expectNoLabelsMatch(t, task, &map[string]string{
   199  		"foo":    "bar",
   200  		"foobar": "bar",
   201  		"baz":    "blah",
   202  	})
   203  
   204  }