github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/pkg/registry/manifest_factory_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 TestMakeManifestNoServices(t *testing.T) {
    25  	registry := MockServiceRegistry{}
    26  	factory := &BasicManifestFactory{
    27  		serviceRegistry: &registry,
    28  	}
    29  
    30  	manifest, err := factory.MakeManifest("machine", Task{
    31  		JSONBase: JSONBase{ID: "foobar"},
    32  		DesiredState: TaskState{
    33  			Manifest: ContainerManifest{
    34  				Containers: []Container{
    35  					Container{
    36  						Name: "foo",
    37  					},
    38  				},
    39  			},
    40  		},
    41  	})
    42  	expectNoError(t, err)
    43  	container := manifest.Containers[0]
    44  	if len(container.Env) != 1 ||
    45  		container.Env[0].Name != "SERVICE_HOST" ||
    46  		container.Env[0].Value != "machine" {
    47  		t.Errorf("Expected one env vars, got: %#v", manifest)
    48  	}
    49  	if manifest.Id != "foobar" {
    50  		t.Errorf("Failed to assign id to manifest: %#v")
    51  	}
    52  }
    53  
    54  func TestMakeManifestServices(t *testing.T) {
    55  	registry := MockServiceRegistry{
    56  		list: ServiceList{
    57  			Items: []Service{
    58  				Service{
    59  					JSONBase: JSONBase{ID: "test"},
    60  					Port:     8080,
    61  				},
    62  			},
    63  		},
    64  	}
    65  	factory := &BasicManifestFactory{
    66  		serviceRegistry: &registry,
    67  	}
    68  
    69  	manifest, err := factory.MakeManifest("machine", Task{
    70  		DesiredState: TaskState{
    71  			Manifest: ContainerManifest{
    72  				Containers: []Container{
    73  					Container{
    74  						Name: "foo",
    75  					},
    76  				},
    77  			},
    78  		},
    79  	})
    80  	expectNoError(t, err)
    81  	container := manifest.Containers[0]
    82  	if len(container.Env) != 2 ||
    83  		container.Env[0].Name != "TEST_SERVICE_PORT" ||
    84  		container.Env[0].Value != "8080" ||
    85  		container.Env[1].Name != "SERVICE_HOST" ||
    86  		container.Env[1].Value != "machine" {
    87  		t.Errorf("Expected 2 env vars, got: %#v", manifest)
    88  	}
    89  }
    90  
    91  func TestMakeManifestServicesExistingEnvVar(t *testing.T) {
    92  	registry := MockServiceRegistry{
    93  		list: ServiceList{
    94  			Items: []Service{
    95  				Service{
    96  					JSONBase: JSONBase{ID: "test"},
    97  					Port:     8080,
    98  				},
    99  			},
   100  		},
   101  	}
   102  	factory := &BasicManifestFactory{
   103  		serviceRegistry: &registry,
   104  	}
   105  
   106  	manifest, err := factory.MakeManifest("machine", Task{
   107  		DesiredState: TaskState{
   108  			Manifest: ContainerManifest{
   109  				Containers: []Container{
   110  					Container{
   111  						Env: []EnvVar{
   112  							EnvVar{
   113  								Name:  "foo",
   114  								Value: "bar",
   115  							},
   116  						},
   117  					},
   118  				},
   119  			},
   120  		},
   121  	})
   122  	expectNoError(t, err)
   123  	container := manifest.Containers[0]
   124  	if len(container.Env) != 3 ||
   125  		container.Env[0].Name != "foo" ||
   126  		container.Env[0].Value != "bar" ||
   127  		container.Env[1].Name != "TEST_SERVICE_PORT" ||
   128  		container.Env[1].Value != "8080" ||
   129  		container.Env[2].Name != "SERVICE_HOST" ||
   130  		container.Env[2].Value != "machine" {
   131  		t.Errorf("Expected no env vars, got: %#v", manifest)
   132  	}
   133  }