github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/cmd/dosa/scope_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package main
    22  
    23  import (
    24  	"os"
    25  	"testing"
    26  
    27  	"github.com/golang/mock/gomock"
    28  	"github.com/pkg/errors"
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/uber-go/dosa"
    31  	"github.com/uber-go/dosa/mocks"
    32  )
    33  
    34  func TestScope_ServiceDefault(t *testing.T) {
    35  	tcs := []struct {
    36  		serviceName string
    37  		expected    string
    38  	}{
    39  		//  service = "" -> default
    40  		{
    41  			expected: _defServiceName,
    42  		},
    43  		//  service = "foo" -> foo
    44  		{
    45  			serviceName: "foo",
    46  			expected:    "foo",
    47  		},
    48  	}
    49  	for _, tc := range tcs {
    50  		for _, cmd := range []string{"create", "drop", "truncate"} {
    51  			os.Args = []string{
    52  				"dosa",
    53  				"--service", tc.serviceName,
    54  				"--connector", "devnull",
    55  				"scope",
    56  				cmd,
    57  				"scope",
    58  			}
    59  			main()
    60  			assert.Equal(t, options.ServiceName, tc.expected)
    61  		}
    62  	}
    63  }
    64  
    65  // There are 3 tests to perform on each scope operator:
    66  // 1 - success case, should call connector once for each provided scope, displaying scope name and operation
    67  // 2 - failure case, connector's API call generated error, provides name of scope that failed
    68  // 3 - failure case, connector could not be initialized
    69  
    70  func TestScope_Create(t *testing.T) {
    71  	ctrl := gomock.NewController(t)
    72  	defer ctrl.Finish()
    73  
    74  	exit = func(r int) {
    75  		assert.Equal(t, 0, r)
    76  	}
    77  	dosa.RegisterConnector("mock", func(dosa.CreationArgs) (dosa.Connector, error) {
    78  		mc := mocks.NewMockConnector(ctrl)
    79  		mc.EXPECT().CreateScope(gomock.Any(), gomock.Any()).Times(4).Return(nil)
    80  		return mc, nil
    81  	})
    82  	os.Args = []string{"dosa", "--connector", "mock", "scope", "create", "one_scope", "two_scope", "three_scope", "four"}
    83  	c := StartCapture()
    84  	main()
    85  	assert.Contains(t, c.stop(false), "\"three_scope\"")
    86  
    87  	dosa.RegisterConnector("mock", func(dosa.CreationArgs) (dosa.Connector, error) {
    88  		mc := mocks.NewMockConnector(ctrl)
    89  		mc.EXPECT().CreateScope(gomock.Any(), gomock.Any()).Times(1).Return(errors.New("oops"))
    90  		return mc, nil
    91  	})
    92  	c = StartCapture()
    93  	exit = func(r int) {
    94  		assert.Equal(t, 1, r)
    95  	}
    96  	os.Args = []string{"dosa", "--connector", "oops", "scope", "drop", "five_fish", "six_fish", "seven_fish", "eight"}
    97  	main()
    98  	assert.Contains(t, c.stop(true), "oops")
    99  }
   100  
   101  func TestScopeDrop_Execute(t *testing.T) {
   102  	ctrl := gomock.NewController(t)
   103  	defer ctrl.Finish()
   104  
   105  	exit = func(r int) {
   106  		assert.Equal(t, 0, r)
   107  	}
   108  	c := StartCapture()
   109  	dosa.RegisterConnector("mock", func(dosa.CreationArgs) (dosa.Connector, error) {
   110  		mc := mocks.NewMockConnector(ctrl)
   111  		mc.EXPECT().DropScope(gomock.Any(), gomock.Any()).Times(4).Return(nil)
   112  		return mc, nil
   113  	})
   114  	os.Args = []string{"dosa", "--connector", "mock", "scope", "drop", "one_fish", "two_fish", "three_fish", "four"}
   115  	main()
   116  	assert.Contains(t, c.stop(false), "\"three_fish\"")
   117  
   118  	c = StartCapture()
   119  	exit = func(r int) {
   120  		assert.Equal(t, 1, r)
   121  	}
   122  	os.Args = []string{"dosa", "--connector", "oops", "scope", "drop", "five_fish", "six_fish", "seven_fish", "eight"}
   123  	main()
   124  	assert.Contains(t, c.stop(true), "oops")
   125  }
   126  
   127  func TestScopeTruncate_Execute(t *testing.T) {
   128  	ctrl := gomock.NewController(t)
   129  	defer ctrl.Finish()
   130  
   131  	// success case
   132  	c := StartCapture()
   133  	exit = func(r int) {
   134  		assert.Equal(t, 0, r)
   135  	}
   136  	dosa.RegisterConnector("mock", func(dosa.CreationArgs) (dosa.Connector, error) {
   137  		mc := mocks.NewMockConnector(ctrl)
   138  		mc.EXPECT().TruncateScope(gomock.Any(), gomock.Any()).Times(4).Return(nil)
   139  		return mc, nil
   140  	})
   141  	os.Args = []string{"dosa", "--connector", "mock", "scope", "truncate", "one_fish", "two_fish", "three_fish", "four"}
   142  	main()
   143  	assert.Contains(t, c.stop(false), "three_fish")
   144  
   145  	// failure case
   146  	c = StartCapture()
   147  	exit = func(r int) {
   148  		assert.Equal(t, 1, r)
   149  	}
   150  	dosa.RegisterConnector("mock", func(dosa.CreationArgs) (dosa.Connector, error) {
   151  		mc := mocks.NewMockConnector(ctrl)
   152  		mc.EXPECT().TruncateScope(gomock.Any(), gomock.Any()).Times(1).Return(&dosa.ErrNotFound{})
   153  		return mc, nil
   154  	})
   155  	os.Args = []string{"dosa", "--connector", "mock", "scope", "truncate", "one_fish", "two_fish", "three_fish", "four"}
   156  	main()
   157  	assert.Contains(t, c.stop(true), "\"one_fish\"")
   158  }