Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
test persist release (#99)
Browse files Browse the repository at this point in the history
* test persist release

* refactor fs
  • Loading branch information
kherroWisc authored May 29, 2018
1 parent 5471c8b commit bfea181
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pkg/specs/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package specs
import (
"context"
"fmt"
"io/ioutil"

"os"
"path/filepath"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"github.com/replicatedcom/ship/pkg/api"
"github.com/replicatedcom/ship/pkg/lifecycle/render/state"
"github.com/replicatedcom/ship/pkg/logger"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
Expand All @@ -36,6 +34,7 @@ type Selector struct {
type Resolver struct {
Logger log.Logger
Client *GraphQLClient
StateManager *state.StateManager
StudioFile string
StudioChannelName string
StudioChannelIcon string
Expand All @@ -50,6 +49,7 @@ func ResolverFromViper(v *viper.Viper) (*Resolver, error) {
return &Resolver{
Logger: logger.FromViper(v),
Client: graphql,
StateManager: state.ManagerFromViper(v),
StudioFile: v.GetString("studio-file"),
StudioChannelName: v.GetString("studio-channel-name"),
StudioChannelIcon: v.GetString("studio-channel-icon"),
Expand Down Expand Up @@ -98,7 +98,7 @@ func (r *Resolver) resolveStudioRelease() (*ShipRelease, error) {
debug := level.Debug(log.With(r.Logger, "method", "resolveStudioSpec"))
debug.Log("phase", "load-specs", "from", "studio-file", "file", r.StudioFile)

specYAML, err := ioutil.ReadFile(r.StudioFile)
specYAML, err := r.StateManager.FS.ReadFile(r.StudioFile)
if err != nil {
return nil, errors.Wrapf(err, "read specs from %s", r.StudioFile)
}
Expand Down Expand Up @@ -136,11 +136,11 @@ func (r *Resolver) resolveCloudRelease(customerID, installationID string) (*Ship

// persistSpec persists last-used YAML to disk at .ship/release.yml
func (r *Resolver) persistSpec(specYAML []byte) error {
if err := os.MkdirAll(filepath.Dir(ReleasePath), 0700); err != nil {
if err := r.StateManager.FS.MkdirAll(filepath.Dir(ReleasePath), 0700); err != nil {
return errors.Wrap(err, "mkdir yaml")
}

if err := ioutil.WriteFile(ReleasePath, specYAML, 0644); err != nil {
if err := r.StateManager.FS.WriteFile(ReleasePath, specYAML, 0644); err != nil {
return errors.Wrap(err, "write yaml file")
}
return nil
Expand Down
29 changes: 29 additions & 0 deletions pkg/specs/resolver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package specs

import (
"testing"
"github.com/go-kit/kit/log"
"github.com/replicatedcom/ship/pkg/lifecycle/render/state"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"reflect"
)

func TestPersistSpec(t *testing.T) {

r := &Resolver{
StateManager: &state.StateManager{
Logger: log.NewNopLogger(),
FS: afero.Afero{Fs: afero.NewMemMapFs()},
},
}

req := require.New(t)

desiredSpec := []byte("my cool spec")
err := r.persistSpec(desiredSpec)
req.NoError(err)

persistedSpec, err := r.StateManager.FS.ReadFile(".ship/release.yml")
req.True(reflect.DeepEqual(desiredSpec, persistedSpec))
}

0 comments on commit bfea181

Please sign in to comment.