github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/version/version_test.go (about) 1 // Copyright (c) 2022 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package version 7 8 import ( 9 "testing" 10 11 "github.com/golang/mock/gomock" 12 "github.com/iotexproject/iotex-proto/golang/iotexapi" 13 "github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi" 14 "github.com/iotexproject/iotex-proto/golang/iotextypes" 15 "github.com/pkg/errors" 16 "github.com/stretchr/testify/require" 17 18 "github.com/iotexproject/iotex-core/ioctl/config" 19 "github.com/iotexproject/iotex-core/ioctl/util" 20 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 21 ) 22 23 func TestVersionCommand(t *testing.T) { 24 require := require.New(t) 25 ctrl := gomock.NewController(t) 26 client := mock_ioctlclient.NewMockClient(ctrl) 27 cfg := config.Config{} 28 apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) 29 response := iotexapi.GetServerMetaResponse{ 30 ServerMeta: &iotextypes.ServerMeta{PackageVersion: "1.0"}, 31 } 32 33 client.EXPECT().SelectTranslation(gomock.Any()).Return("", config.English).AnyTimes() 34 endpoint := "111:222:333:444:5678" 35 insecure := true 36 callbackEndpoint := func(cb func(*string, string, string, string)) { 37 cb(&endpoint, "endpoint", endpoint, "endpoint usage") 38 } 39 callbackInsecure := func(cb func(*bool, string, bool, string)) { 40 cb(&insecure, "insecure", !insecure, "insecure usage") 41 } 42 client.EXPECT().SetEndpointWithFlag(gomock.Any()).Do(callbackEndpoint).Times(3) 43 client.EXPECT().SetInsecureWithFlag(gomock.Any()).Do(callbackInsecure).Times(3) 44 client.EXPECT().Config().Return(cfg).Times(1) 45 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(2) 46 47 t.Run("get ioctl version", func(t *testing.T) { 48 expectedValue := "packageVersion:\"1.0\"" 49 apiServiceClient.EXPECT().GetServerMeta(gomock.Any(), gomock.Any()).Return(&response, nil).Times(1) 50 51 cmd := NewVersionCmd(client) 52 result, err := util.ExecuteCmd(cmd) 53 require.NoError(err) 54 require.Contains(result, expectedValue) 55 }) 56 57 t.Run("failed to get version from server", func(t *testing.T) { 58 expectedErr := errors.New("failed to get version from server") 59 apiServiceClient.EXPECT().GetServerMeta(gomock.Any(), gomock.Any()).Return(nil, expectedErr).Times(1) 60 61 cmd := NewVersionCmd(client) 62 _, err := util.ExecuteCmd(cmd) 63 require.Error(err) 64 require.Contains(err.Error(), expectedErr.Error()) 65 }) 66 67 t.Run("use \"ioctl config set endpoint\" to config endpoint first", func(t *testing.T) { 68 expectedErr := errors.New("use \"ioctl config set endpoint\" to config endpoint first") 69 client.EXPECT().APIServiceClient().Return(nil, expectedErr).Times(1) 70 apiServiceClient.EXPECT().GetServerMeta(gomock.Any(), gomock.Any()).Return(&response, nil).AnyTimes() 71 72 cmd := NewVersionCmd(client) 73 _, err := util.ExecuteCmd(cmd) 74 require.Error(err) 75 require.Contains(err.Error(), expectedErr.Error()) 76 }) 77 }