github.com/ystia/yorc/v4@v4.3.0/plugin/config_test.go (about) 1 // Copyright 2018 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package plugin 16 17 import ( 18 "errors" 19 "testing" 20 21 plugin "github.com/hashicorp/go-plugin" 22 "github.com/stretchr/testify/require" 23 24 "github.com/ystia/yorc/v4/config" 25 ) 26 27 type mockConfigManager struct { 28 setupConfigCalled bool 29 conf config.Configuration 30 deploymentID string 31 } 32 33 func (m *mockConfigManager) SetupConfig(cfg config.Configuration) error { 34 m.setupConfigCalled = true 35 m.conf = cfg 36 37 if m.conf.Consul.Address == "testFailure" { 38 return NewRPCError(errors.New("a failure occurred during plugin exec operation")) 39 } 40 return nil 41 } 42 43 func setupConfigManagerTestEnv(t *testing.T) (*mockConfigManager, *plugin.RPCClient, 44 ConfigManager) { 45 46 mock := new(mockConfigManager) 47 client, _ := plugin.TestPluginRPCConn( 48 t, 49 map[string]plugin.Plugin{ 50 ConfigManagerPluginName: &ConfigManagerPlugin{mock}, 51 }, 52 nil) 53 54 raw, err := client.Dispense(ConfigManagerPluginName) 55 require.Nil(t, err) 56 57 cm := raw.(ConfigManager) 58 59 return mock, client, cm 60 61 } 62 func TestConfigManagerSetupConfig(t *testing.T) { 63 t.Parallel() 64 mock, client, cm := setupConfigManagerTestEnv(t) 65 defer client.Close() 66 err := cm.SetupConfig(config.Configuration{Consul: config.Consul{Address: "test", Datacenter: "testdc"}}) 67 require.Nil(t, err) 68 require.True(t, mock.setupConfigCalled) 69 require.Equal(t, "test", mock.conf.Consul.Address) 70 require.Equal(t, "testdc", mock.conf.Consul.Datacenter) 71 } 72 73 func TestConfigManagerSetupConfigWithFailure(t *testing.T) { 74 t.Parallel() 75 _, client, cm := setupConfigManagerTestEnv(t) 76 defer client.Close() 77 err := cm.SetupConfig(config.Configuration{Consul: config.Consul{Address: "testFailure", Datacenter: "testdc"}}) 78 require.Error(t, err, "An error was expected during executing plugin operation") 79 }