github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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 service API for testing the service
    15  // update command.
    16  type fakeServiceAPI struct {
    17  	serviceName string
    18  	charmName   string
    19  	values      map[string]interface{}
    20  	config      string
    21  	err         error
    22  }
    23  
    24  func (f *fakeServiceAPI) Update(args params.ServiceUpdate) error {
    25  	if f.err != nil {
    26  		return f.err
    27  	}
    28  
    29  	if args.ServiceName != f.serviceName {
    30  		return errors.NotFoundf("service %q", args.ServiceName)
    31  	}
    32  
    33  	f.config = args.SettingsYAML
    34  	return nil
    35  }
    36  
    37  func (f *fakeServiceAPI) Close() error {
    38  	return nil
    39  }
    40  
    41  func (f *fakeServiceAPI) Get(service string) (*params.ServiceGetResults, error) {
    42  	if service != f.serviceName {
    43  		return nil, errors.NotFoundf("service %q", service)
    44  	}
    45  
    46  	configInfo := make(map[string]interface{})
    47  	for k, v := range f.values {
    48  		configInfo[k] = map[string]interface{}{
    49  			"description": fmt.Sprintf("Specifies %s", k),
    50  			"type":        fmt.Sprintf("%T", v),
    51  			"value":       v,
    52  		}
    53  	}
    54  
    55  	return &params.ServiceGetResults{
    56  		Service: f.serviceName,
    57  		Charm:   f.charmName,
    58  		Config:  configInfo,
    59  	}, nil
    60  }
    61  
    62  func (f *fakeServiceAPI) Set(service string, options map[string]string) error {
    63  	if f.err != nil {
    64  		return f.err
    65  	}
    66  
    67  	if service != f.serviceName {
    68  		return errors.NotFoundf("service %q", service)
    69  	}
    70  
    71  	if f.values == nil {
    72  		f.values = make(map[string]interface{})
    73  	}
    74  	for k, v := range options {
    75  		f.values[k] = v
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  func (f *fakeServiceAPI) Unset(service string, options []string) error {
    82  	if f.err != nil {
    83  		return f.err
    84  	}
    85  
    86  	if service != f.serviceName {
    87  		return errors.NotFoundf("service %q", service)
    88  	}
    89  
    90  	// Verify all options before unsetting any of them.
    91  	for _, name := range options {
    92  		if _, ok := f.values[name]; !ok {
    93  			return fmt.Errorf("unknown option %q", name)
    94  		}
    95  	}
    96  
    97  	for _, name := range options {
    98  		delete(f.values, name)
    99  	}
   100  
   101  	return nil
   102  }