github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/runtime/kubernetes/client/util_test.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/util/kubernetes/client/util_test.go
    14  
    15  package client
    16  
    17  import (
    18  	"bytes"
    19  	"testing"
    20  
    21  	"github.com/tickoalcantara12/micro/v3/service/runtime"
    22  )
    23  
    24  func TestTemplates(t *testing.T) {
    25  	srv := &runtime.Service{Name: "foo", Version: "123"}
    26  	opts := &runtime.CreateOptions{Type: "service", Namespace: "default"}
    27  
    28  	// Render default service
    29  	s := NewService(srv, opts)
    30  	bs := new(bytes.Buffer)
    31  	if err := renderTemplate(templates["service"], bs, s); err != nil {
    32  		t.Errorf("Failed to render kubernetes service: %v", err)
    33  	}
    34  
    35  	// Render default deployment
    36  	d := NewDeployment(srv, opts)
    37  	bd := new(bytes.Buffer)
    38  	if err := renderTemplate(templates["deployment"], bd, d); err != nil {
    39  		t.Errorf("Failed to render kubernetes deployment: %v", err)
    40  	}
    41  }
    42  
    43  func TestFormatName(t *testing.T) {
    44  	testCases := []struct {
    45  		name   string
    46  		expect string
    47  	}{
    48  		{"foobar", "foobar"},
    49  		{"foo-bar", "foo-bar"},
    50  		{"foo.bar", "foo-bar"},
    51  		{"Foo.Bar", "foo-bar"},
    52  		{"go.micro.foo.bar", "go-micro-foo-bar"},
    53  		{"go.micro.foo.bar", "go-micro-foo-bar"},
    54  		{"foo/bar_baz", "foo-bar-baz"},
    55  	}
    56  
    57  	for _, test := range testCases {
    58  		v := Format(test.name)
    59  		if v != test.expect {
    60  			t.Fatalf("Expected name %s for %s got: %s", test.expect, test.name, v)
    61  		}
    62  	}
    63  }