github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/model/fakeenv_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package model_test
     5  
     6  import (
     7  	gc "gopkg.in/check.v1"
     8  
     9  	"github.com/juju/juju/environs/config"
    10  	"github.com/juju/juju/testing"
    11  )
    12  
    13  // ModelConfig related fake environment for testing.
    14  
    15  type fakeEnvSuite struct {
    16  	testing.FakeJujuXDGDataHomeSuite
    17  	fake *fakeEnvAPI
    18  }
    19  
    20  func (s *fakeEnvSuite) SetUpTest(c *gc.C) {
    21  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    22  	s.fake = &fakeEnvAPI{
    23  		values: map[string]interface{}{
    24  			"name":    "test-model",
    25  			"special": "special value",
    26  			"running": true,
    27  		},
    28  		defaults: config.ConfigValues{
    29  			"attr":  {Value: "foo", Source: "default"},
    30  			"attr2": {Value: "bar", Source: "controller"},
    31  			"attr3": {Value: "baz", Source: "region"},
    32  		},
    33  	}
    34  }
    35  
    36  type fakeEnvAPI struct {
    37  	values        map[string]interface{}
    38  	cloud, region string
    39  	defaults      config.ConfigValues
    40  	err           error
    41  	keys          []string
    42  }
    43  
    44  func (f *fakeEnvAPI) Close() error {
    45  	return nil
    46  }
    47  
    48  func (f *fakeEnvAPI) ModelGet() (map[string]interface{}, error) {
    49  	return f.values, nil
    50  }
    51  
    52  func (f *fakeEnvAPI) ModelGetWithMetadata() (config.ConfigValues, error) {
    53  	result := make(config.ConfigValues)
    54  	for name, val := range f.values {
    55  		result[name] = config.ConfigValue{Value: val, Source: "model"}
    56  	}
    57  	return result, nil
    58  }
    59  
    60  func (f *fakeEnvAPI) ModelSet(config map[string]interface{}) error {
    61  	f.values = config
    62  	return f.err
    63  }
    64  
    65  func (f *fakeEnvAPI) ModelUnset(keys ...string) error {
    66  	f.keys = keys
    67  	return f.err
    68  }
    69  
    70  // ModelDefaults related fake environment for testing.
    71  
    72  type fakeModelDefaultEnvSuite struct {
    73  	testing.FakeJujuXDGDataHomeSuite
    74  	fake *fakeModelDefaultsAPI
    75  }
    76  
    77  func (s *fakeModelDefaultEnvSuite) SetUpTest(c *gc.C) {
    78  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    79  	s.fake = &fakeModelDefaultsAPI{
    80  		values: map[string]interface{}{
    81  			"name":    "test-model",
    82  			"special": "special value",
    83  			"running": true,
    84  		},
    85  		defaults: config.ModelDefaultAttributes{
    86  			"attr": {Default: "foo"},
    87  			"attr2": {
    88  				Controller: "bar",
    89  				Regions: []config.RegionDefaultValue{{
    90  					"dummy-region",
    91  					"dummy-value"}}},
    92  		},
    93  	}
    94  }
    95  
    96  type fakeModelDefaultsAPI struct {
    97  	values        map[string]interface{}
    98  	cloud, region string
    99  	defaults      config.ModelDefaultAttributes
   100  	err           error
   101  	keys          []string
   102  }
   103  
   104  func (f *fakeModelDefaultsAPI) Close() error {
   105  	return nil
   106  }
   107  
   108  func (f *fakeModelDefaultsAPI) ModelGet() (map[string]interface{}, error) {
   109  	return f.values, nil
   110  }
   111  
   112  func (f *fakeModelDefaultsAPI) ModelDefaults() (config.ModelDefaultAttributes, error) {
   113  	return f.defaults, nil
   114  }
   115  
   116  func (f *fakeModelDefaultsAPI) SetModelDefaults(cloud, region string, cfg map[string]interface{}) error {
   117  	if f.err != nil {
   118  		return f.err
   119  	}
   120  	f.cloud = cloud
   121  	f.region = region
   122  	for name, val := range cfg {
   123  		f.defaults[name] = config.AttributeDefaultValues{Controller: val}
   124  	}
   125  	return nil
   126  }
   127  
   128  func (f *fakeModelDefaultsAPI) UnsetModelDefaults(cloud, region string, keys ...string) error {
   129  	if f.err != nil {
   130  		return f.err
   131  	}
   132  	f.cloud = cloud
   133  	f.region = region
   134  	for _, key := range keys {
   135  		delete(f.defaults, key)
   136  	}
   137  	return nil
   138  }
   139  
   140  func (f *fakeModelDefaultsAPI) ModelSet(config map[string]interface{}) error {
   141  	f.values = config
   142  	return f.err
   143  }
   144  
   145  func (f *fakeModelDefaultsAPI) ModelUnset(keys ...string) error {
   146  	f.keys = keys
   147  	return f.err
   148  }