github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/service/fakeservice_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package service_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/errors"
    10  
    11  	"github.com/juju/juju/apiserver/params"
    12  )
    13  
    14  // fakeServiceAPI is the fake client API for testing the service set,
    15  // get and unset commands.  It implements the following interfaces:
    16  // SetServiceAPI, UnsetServiceAPI and GetServiceAPI
    17  type fakeServiceAPI struct {
    18  	values    map[string]interface{}
    19  	servName  string
    20  	charmName string
    21  	config    string
    22  	err       error
    23  }
    24  
    25  func (f *fakeServiceAPI) Close() error {
    26  	return nil
    27  }
    28  
    29  func (f *fakeServiceAPI) ServiceSetYAML(service string, yaml string) error {
    30  	if f.err != nil {
    31  		return f.err
    32  	}
    33  
    34  	if service != f.servName {
    35  		return errors.NotFoundf("service %q", service)
    36  	}
    37  
    38  	f.config = yaml
    39  	return nil
    40  }
    41  
    42  func (f *fakeServiceAPI) ServiceGet(service string) (*params.ServiceGetResults, error) {
    43  	if service != f.servName {
    44  		return nil, errors.NotFoundf("service %q", service)
    45  	}
    46  
    47  	configInfo := make(map[string]interface{})
    48  	for k, v := range f.values {
    49  		configInfo[k] = map[string]interface{}{
    50  			"description": fmt.Sprintf("Specifies %s", k),
    51  			"type":        fmt.Sprintf("%T", v),
    52  			"value":       v,
    53  		}
    54  	}
    55  
    56  	return &params.ServiceGetResults{
    57  		Service: f.servName,
    58  		Charm:   f.charmName,
    59  		Config:  configInfo,
    60  	}, nil
    61  }
    62  
    63  func (f *fakeServiceAPI) ServiceSet(service string, options map[string]string) error {
    64  	if f.err != nil {
    65  		return f.err
    66  	}
    67  
    68  	if service != f.servName {
    69  		return errors.NotFoundf("service %q", service)
    70  	}
    71  
    72  	for k, v := range options {
    73  		f.values[k] = v
    74  	}
    75  
    76  	return nil
    77  }
    78  
    79  func (f *fakeServiceAPI) ServiceUnset(service string, options []string) error {
    80  	if f.err != nil {
    81  		return f.err
    82  	}
    83  
    84  	if service != f.servName {
    85  		return errors.NotFoundf("service %q", service)
    86  	}
    87  
    88  	// Verify all options before unsetting any of them.
    89  	for _, name := range options {
    90  		if _, ok := f.values[name]; !ok {
    91  			return fmt.Errorf("unknown option %q", name)
    92  		}
    93  	}
    94  
    95  	for _, name := range options {
    96  		delete(f.values, name)
    97  	}
    98  
    99  	return nil
   100  }