vitess.io/vitess@v0.16.2/go/vt/vtadmin/cluster/discovery/discovery_test.go (about)

     1  /*
     2  Copyright 2020 The Vitess Authors.
     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  
    17  package discovery
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  
    26  	vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin"
    27  )
    28  
    29  func TestNew(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	tests := []struct {
    33  		name string
    34  		impl string
    35  		err  error
    36  		typ  Discovery
    37  	}{
    38  		{
    39  			name: "success",
    40  			impl: "consul",
    41  			err:  nil,
    42  			typ:  &ConsulDiscovery{},
    43  		},
    44  		{
    45  			name: "unregistered",
    46  			impl: "unregistered",
    47  			err:  ErrImplementationNotRegistered,
    48  			typ:  nil,
    49  		},
    50  	}
    51  
    52  	for _, tt := range tests {
    53  		tt := tt
    54  
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			t.Parallel()
    57  
    58  			disco, err := New(tt.impl, &vtadminpb.Cluster{Id: "testid", Name: "testcluster"}, []string{})
    59  			if tt.err != nil {
    60  				assert.Error(t, err, tt.err.Error())
    61  				return
    62  			}
    63  
    64  			assert.NoError(t, err)
    65  			assert.IsType(t, tt.typ, disco)
    66  		})
    67  	}
    68  }
    69  
    70  func TestRegister(t *testing.T) {
    71  	t.Parallel()
    72  
    73  	// Use a timestamp to allow running tests with `-count=N`.
    74  	ts := time.Now().UnixNano()
    75  	factoryName := fmt.Sprintf("testfactory-%d", ts)
    76  
    77  	Register(factoryName, nil)
    78  
    79  	defer func() {
    80  		err := recover()
    81  		assert.NotNil(t, err)
    82  		assert.IsType(t, "", err)
    83  		assert.Contains(t, err.(string), "factory already registered")
    84  	}()
    85  
    86  	// this one panics
    87  	Register(factoryName, nil)
    88  	assert.Equal(t, 1, 2, "double register should have panicked")
    89  }