github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/pkg/registry/endpoints_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  	"fmt"
    20  	"testing"
    21  
    22  	. "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
    23  )
    24  
    25  func TestSyncEndpointsEmpty(t *testing.T) {
    26  	serviceRegistry := MockServiceRegistry{}
    27  	taskRegistry := MockTaskRegistry{}
    28  
    29  	endpoints := MakeEndpointController(&serviceRegistry, &taskRegistry)
    30  	err := endpoints.SyncServiceEndpoints()
    31  	expectNoError(t, err)
    32  }
    33  
    34  func TestSyncEndpointsError(t *testing.T) {
    35  	serviceRegistry := MockServiceRegistry{
    36  		err: fmt.Errorf("Test Error"),
    37  	}
    38  	taskRegistry := MockTaskRegistry{}
    39  
    40  	endpoints := MakeEndpointController(&serviceRegistry, &taskRegistry)
    41  	err := endpoints.SyncServiceEndpoints()
    42  	if err != serviceRegistry.err {
    43  		t.Errorf("Errors don't match: %#v %#v", err, serviceRegistry.err)
    44  	}
    45  }
    46  
    47  func TestSyncEndpointsItems(t *testing.T) {
    48  	serviceRegistry := MockServiceRegistry{
    49  		list: ServiceList{
    50  			Items: []Service{
    51  				Service{
    52  					Labels: map[string]string{
    53  						"foo": "bar",
    54  					},
    55  				},
    56  			},
    57  		},
    58  	}
    59  	taskRegistry := MockTaskRegistry{
    60  		tasks: []Task{
    61  			Task{
    62  				DesiredState: TaskState{
    63  					Manifest: ContainerManifest{
    64  						Containers: []Container{
    65  							Container{
    66  								Ports: []Port{
    67  									Port{
    68  										HostPort: 8080,
    69  									},
    70  								},
    71  							},
    72  						},
    73  					},
    74  				},
    75  			},
    76  		},
    77  	}
    78  
    79  	endpoints := MakeEndpointController(&serviceRegistry, &taskRegistry)
    80  	err := endpoints.SyncServiceEndpoints()
    81  	expectNoError(t, err)
    82  	if len(serviceRegistry.endpoints.Endpoints) != 1 {
    83  		t.Errorf("Unexpected endpoints update: %#v", serviceRegistry.endpoints)
    84  	}
    85  }
    86  
    87  func TestSyncEndpointsTaskError(t *testing.T) {
    88  	serviceRegistry := MockServiceRegistry{
    89  		list: ServiceList{
    90  			Items: []Service{
    91  				Service{
    92  					Labels: map[string]string{
    93  						"foo": "bar",
    94  					},
    95  				},
    96  			},
    97  		},
    98  	}
    99  	taskRegistry := MockTaskRegistry{
   100  		err: fmt.Errorf("test error."),
   101  	}
   102  
   103  	endpoints := MakeEndpointController(&serviceRegistry, &taskRegistry)
   104  	err := endpoints.SyncServiceEndpoints()
   105  	if err == nil {
   106  		t.Error("Unexpected non-error")
   107  	}
   108  }