Skip to content

Commit

Permalink
load init file when starting a session
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Ratier committed Feb 6, 2024
1 parent c8ad650 commit 4503978
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
9 changes: 1 addition & 8 deletions server/mock_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ func NewMockServer(cfg config.Config) (*http.Server, services.Mocks) {
if err != nil {
log.Error("Unable to load sessions: ", err)
}
mockServices := services.NewMocks(sessions, cfg.HistoryMaxRetention, persistence)

if cfg.InitFilePath != "" {
err = mockServices.LoadInitializationFile(cfg.InitFilePath)
if err != nil {
log.Error("Unable to load intialization file: ", err)
}
}
mockServices := services.NewMocks(sessions, cfg.HistoryMaxRetention, persistence, cfg.InitFilePath)

mockServerEngine.HideBanner = true
mockServerEngine.HidePort = true
Expand Down
42 changes: 24 additions & 18 deletions server/services/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var (

type Mocks interface {
AddMock(sessionID string, mock *types.Mock) (*types.Mock, error)
LoadInitializationFile(string) error
GetMocks(sessionID string) (types.Mocks, error)
GetMockByID(sessionID, id string) (*types.Mock, error)
LockMocks(ids []string) types.Mocks
Expand All @@ -43,52 +42,47 @@ type mocks struct {
mu sync.Mutex
historyRetention int
persistence Persistence
initFilePath string
}

func NewMocks(sessions types.Sessions, historyRetention int, persistence Persistence) Mocks {
func NewMocks(sessions types.Sessions, historyRetention int, persistence Persistence, initFilePath string) Mocks {
s := &mocks{
sessions: types.Sessions{},
historyRetention: historyRetention,
persistence: persistence,
initFilePath: initFilePath,
}
if sessions != nil {
s.sessions = sessions
}
return s
}

func (s *mocks) LoadInitializationFile(filePath string) error {
func (s *mocks) GetMocksFromInitializationFile(filePath string) (*types.Mocks, error) {
mocksFile, err := os.Open(filePath)
if err != nil {
return err
return nil, err
}
defer mocksFile.Close()
bytes, err := io.ReadAll(mocksFile)
if err != nil {
return err
return nil, err
}
var mocks types.Mocks
err = yaml.Unmarshal(bytes, &mocks)
if err != nil {
return err
return nil, err
}

session := s.GetLastSession()
newMockIDS := []string{}

var ret types.Mocks
// mocks are stored as a stack so we need to reverse the list from mocks file
for i := len(mocks) - 1; i >= 0; i-- {
mock, err := s.AddMock(session.ID, mocks[i])
if err != nil {
return err
}
newMockIDS = append(newMockIDS, mock.State.ID)

m := mocks[i]
m.Init()
ret = append(ret, m)
}

s.LockMocks(newMockIDS)

return nil
return &ret, nil
}

func (s *mocks) AddMock(sessionID string, newMock *types.Mock) (*types.Mock, error) {
Expand Down Expand Up @@ -247,6 +241,18 @@ func (s *mocks) NewSession(name string) *types.Session {
s.mu.Lock()
defer s.mu.Unlock()

if s.initFilePath != "" {
fmt.Printf("Init\n\n")

initMocks, err := s.GetMocksFromInitializationFile(s.initFilePath)
if err != nil {
fmt.Printf("Error is: %s\n\n", err)
return nil
}

mocks = append(*initMocks, mocks...)
}

session := &types.Session{
ID: shortid.MustGenerate(),
Name: name,
Expand Down

0 comments on commit 4503978

Please sign in to comment.