diff --git a/api/blueprint/bootstrapping_api_mocked.go b/api/blueprint/bootstrapping_api_mocked.go index 28bf3c3..29ef6b8 100644 --- a/api/blueprint/bootstrapping_api_mocked.go +++ b/api/blueprint/bootstrapping_api_mocked.go @@ -1,3 +1,316 @@ package blueprint -// TODO +import ( + "encoding/json" + "github.com/ingrammicro/concerto/api/types" + "github.com/ingrammicro/concerto/utils" + "github.com/stretchr/testify/assert" + "testing" + "fmt" +) + +// GetBootstrappingConfigurationMocked test mocked function +func GetBootstrappingConfigurationMocked(t *testing.T, bcConfIn *types.BootstrappingConfiguration) *types.BootstrappingConfiguration { + + assert := assert.New(t) + + // wire up + cs := &utils.MockConcertoService{} + ds, err := NewBootstrappingService(cs) + assert.Nil(err, "Couldn't load bootstrapping service") + assert.NotNil(ds, "Bootstrapping service not instanced") + + // to json + dIn, err := json.Marshal(bcConfIn) + assert.Nil(err, "Bootstrapping test data corrupted") + + // call service + cs.On("Get", "/blueprint/configuration").Return(dIn, 200, nil) + bcConfOut, status, err := ds.GetBootstrappingConfiguration() + assert.Nil(err, "Error getting bootstrapping configuration") + assert.Equal(status, 200, "GetBootstrappingConfiguration returned invalid response") + assert.Equal(bcConfIn, bcConfOut, "GetBootstrappingConfiguration returned different services") + return bcConfOut +} + +// GetBootstrappingConfigurationFailErrMocked test mocked function +func GetBootstrappingConfigurationFailErrMocked(t *testing.T, bcConfIn *types.BootstrappingConfiguration) *types.BootstrappingConfiguration { + + assert := assert.New(t) + + // wire up + cs := &utils.MockConcertoService{} + ds, err := NewBootstrappingService(cs) + assert.Nil(err, "Couldn't load bootstrapping service") + assert.NotNil(ds, "Bootstrapping service not instanced") + + // to json + dIn, err := json.Marshal(bcConfIn) + assert.Nil(err, "Bootstrapping test data corrupted") + + // call service + cs.On("Get", "/blueprint/configuration").Return(dIn, 404, fmt.Errorf("Mocked error")) + bcConfOut, _, err := ds.GetBootstrappingConfiguration() + + assert.NotNil(err, "We are expecting an error") + assert.Nil(bcConfOut, "Expecting nil output") + assert.Equal(err.Error(), "Mocked error", "Error should be 'Mocked error'") + + return bcConfOut +} + +// GetBootstrappingConfigurationFailStatusMocked test mocked function +func GetBootstrappingConfigurationFailStatusMocked(t *testing.T, bcConfIn *types.BootstrappingConfiguration) *types.BootstrappingConfiguration { + + assert := assert.New(t) + + // wire up + cs := &utils.MockConcertoService{} + ds, err := NewBootstrappingService(cs) + assert.Nil(err, "Couldn't load bootstrapping service") + assert.NotNil(ds, "Bootstrapping service not instanced") + + // to json + dIn, err := json.Marshal(bcConfIn) + assert.Nil(err, "Bootstrapping test data corrupted") + + // call service + cs.On("Get", "/blueprint/configuration").Return(dIn, 499, nil) + bcConfOut, status, err := ds.GetBootstrappingConfiguration() + + assert.NotNil(err, "We are expecting an status code error") + assert.Nil(bcConfOut, "Expecting nil output") + assert.Equal(499, status, "Expecting http code 499") + assert.Contains(err.Error(), "499", "Error should contain http code 499") + + return bcConfOut +} + +// GetBootstrappingConfigurationFailJSONMocked test mocked function +func GetBootstrappingConfigurationFailJSONMocked(t *testing.T, bcConfIn *types.BootstrappingConfiguration) *types.BootstrappingConfiguration { + + assert := assert.New(t) + + // wire up + cs := &utils.MockConcertoService{} + ds, err := NewBootstrappingService(cs) + assert.Nil(err, "Couldn't load bootstrapping service") + assert.NotNil(ds, "Bootstrapping service not instanced") + + // wrong json + dIn := []byte{10, 20, 30} + + // call service + cs.On("Get", "/blueprint/configuration").Return(dIn, 200, nil) + bcConfOut, _, err := ds.GetBootstrappingConfiguration() + + assert.NotNil(err, "We are expecting a marshalling error") + assert.Nil(bcConfOut, "Expecting nil output") + + return bcConfOut +} + +// ReportBootstrappingAppliedConfigurationMocked test mocked function +func ReportBootstrappingAppliedConfigurationMocked(t *testing.T, commandIn *types.BootstrappingConfiguration) { + + assert := assert.New(t) + + // wire up + cs := &utils.MockConcertoService{} + ds, err := NewBootstrappingService(cs) + assert.Nil(err, "Couldn't load bootstrapping service") + assert.NotNil(ds, "Bootstrapping service not instanced") + + // to json + dOut, err := json.Marshal(commandIn) + assert.Nil(err, "Bootstrapping test data corrupted") + + // call service + payload := make(map[string]interface{}) + cs.On("Put", fmt.Sprintf("/blueprint/applied_configuration"), &payload).Return(dOut, 200, nil) + err = ds.ReportBootstrappingAppliedConfiguration(&payload) + assert.Nil(err, "Error getting bootstrapping command") +} + +// ReportBootstrappingAppliedConfigurationFailErrMocked test mocked function +func ReportBootstrappingAppliedConfigurationFailErrMocked(t *testing.T, commandIn *types.BootstrappingConfiguration) { + + assert := assert.New(t) + + // wire up + cs := &utils.MockConcertoService{} + ds, err := NewBootstrappingService(cs) + assert.Nil(err, "Couldn't load bootstrapping service") + assert.NotNil(ds, "Bootstrapping service not instanced") + + // to json + dIn, err := json.Marshal(commandIn) + assert.Nil(err, "Bootstrapping test data corrupted") + + dIn = nil + + // call service + payload := make(map[string]interface{}) + cs.On("Put", fmt.Sprintf("/blueprint/applied_configuration"), &payload).Return(dIn, 400, fmt.Errorf("Mocked error")) + err = ds.ReportBootstrappingAppliedConfiguration(&payload) + assert.NotNil(err, "We are expecting an error") + assert.Equal(err.Error(), "Mocked error", "Error should be 'Mocked error'") +} + +// ReportBootstrappingAppliedConfigurationFailStatusMocked test mocked function +func ReportBootstrappingAppliedConfigurationFailStatusMocked(t *testing.T, commandIn *types.BootstrappingConfiguration) { + + assert := assert.New(t) + + // wire up + cs := &utils.MockConcertoService{} + ds, err := NewBootstrappingService(cs) + assert.Nil(err, "Couldn't load bootstrapping service") + assert.NotNil(ds, "Bootstrapping service not instanced") + + // to json + dIn, err := json.Marshal(commandIn) + assert.Nil(err, "Bootstrapping test data corrupted") + + dIn = nil + + // call service + payload := make(map[string]interface{}) + cs.On("Put", fmt.Sprintf("/blueprint/applied_configuration"), &payload).Return(dIn, 499, fmt.Errorf("Error 499 Mocked error")) + err = ds.ReportBootstrappingAppliedConfiguration(&payload) + assert.NotNil(err, "We are expecting a status code error") + assert.Contains(err.Error(), "499", "Error should contain http code 499") +} + +// ReportBootstrappingAppliedConfigurationFailJSONMocked test mocked function +func ReportBootstrappingAppliedConfigurationFailJSONMocked(t *testing.T, commandIn *types.BootstrappingConfiguration) { + + assert := assert.New(t) + + // wire up + cs := &utils.MockConcertoService{} + ds, err := NewBootstrappingService(cs) + assert.Nil(err, "Couldn't load bootstrapping service") + assert.NotNil(ds, "Bootstrapping service not instanced") + + // wrong json + dIn := []byte{0} + + // call service + payload := make(map[string]interface{}) + cs.On("Put", fmt.Sprintf("/blueprint/applied_configuration"), &payload).Return(dIn, 499, nil) + err = ds.ReportBootstrappingAppliedConfiguration(&payload) + assert.Contains(err.Error(), "499", "Error should contain http code 499") +} + +// ReportBootstrappingLogMocked test mocked function +func ReportBootstrappingLogMocked(t *testing.T, commandIn *types.BootstrappingContinuousReport) *types.BootstrappingContinuousReport { + + assert := assert.New(t) + + // wire up + cs := &utils.MockConcertoService{} + ds, err := NewBootstrappingService(cs) + assert.Nil(err, "Couldn't load bootstrapping service") + assert.NotNil(ds, "Bootstrapping service not instanced") + + // to json + dOut, err := json.Marshal(commandIn) + assert.Nil(err, "Bootstrapping test data corrupted") + + // call service + payload := make(map[string]interface{}) + cs.On("Post", fmt.Sprintf("/blueprint/bootstrap_logs"), &payload).Return(dOut, 200, nil) + commandOut, status, err := ds.ReportBootstrappingLog(&payload) + + assert.Nil(err, "Error posting report command") + assert.Equal(status, 200, "ReportBootstrappingLog returned invalid response") + assert.Equal(commandOut.Stdout, "Bootstrap log created", "ReportBootstrapLog returned unexpected message") + + return commandOut +} + +// ReportBootstrappingLogFailErrMocked test mocked function +func ReportBootstrappingLogFailErrMocked(t *testing.T, commandIn *types.BootstrappingContinuousReport) *types.BootstrappingContinuousReport { + + assert := assert.New(t) + + // wire up + cs := &utils.MockConcertoService{} + ds, err := NewBootstrappingService(cs) + assert.Nil(err, "Couldn't load bootstrapping service") + assert.NotNil(ds, "Bootstrapping service not instanced") + + // to json + dIn, err := json.Marshal(commandIn) + assert.Nil(err, "Bootstrapping test data corrupted") + + dIn = nil + + // call service + payload := make(map[string]interface{}) + cs.On("Post", fmt.Sprintf("/blueprint/bootstrap_logs"), &payload).Return(dIn, 400, fmt.Errorf("Mocked error")) + commandOut, _, err := ds.ReportBootstrappingLog(&payload) + + assert.NotNil(err, "We are expecting an error") + assert.Nil(commandOut, "Expecting nil output") + assert.Equal(err.Error(), "Mocked error", "Error should be 'Mocked error'") + + return commandOut +} + +// ReportBootstrappingLogFailStatusMocked test mocked function +func ReportBootstrappingLogFailStatusMocked(t *testing.T, commandIn *types.BootstrappingContinuousReport) *types.BootstrappingContinuousReport { + + assert := assert.New(t) + + // wire up + cs := &utils.MockConcertoService{} + ds, err := NewBootstrappingService(cs) + assert.Nil(err, "Couldn't load bootstrapping service") + assert.NotNil(ds, "Bootstrapping service not instanced") + + // to json + dIn, err := json.Marshal(commandIn) + assert.Nil(err, "Bootstrapping test data corrupted") + + dIn = nil + + // call service + payload := make(map[string]interface{}) + cs.On("Post", fmt.Sprintf("/blueprint/bootstrap_logs"), &payload).Return(dIn, 499, fmt.Errorf("Error 499 Mocked error")) + commandOut, status, err := ds.ReportBootstrappingLog(&payload) + + assert.Equal(status, 499, "ReportBootstrappingLog returned an unexpected status code") + assert.NotNil(err, "We are expecting a status code error") + assert.Nil(commandOut, "Expecting nil output") + assert.Contains(err.Error(), "499", "Error should contain http code 499") + + return commandOut +} + +// ReportBootstrappingLogFailJSONMocked test mocked function +func ReportBootstrappingLogFailJSONMocked(t *testing.T, commandIn *types.BootstrappingContinuousReport) *types.BootstrappingContinuousReport { + + assert := assert.New(t) + + // wire up + cs := &utils.MockConcertoService{} + ds, err := NewBootstrappingService(cs) + assert.Nil(err, "Couldn't load bootstrapping service") + assert.NotNil(ds, "Bootstrapping service not instanced") + + // wrong json + dIn := []byte{10, 20, 30} + + // call service + payload := make(map[string]interface{}) + cs.On("Post", fmt.Sprintf("/blueprint/bootstrap_logs"), &payload).Return(dIn, 200, nil) + commandOut, _, err := ds.ReportBootstrappingLog(&payload) + + assert.NotNil(err, "We are expecting a marshalling error") + assert.Nil(commandOut, "Expecting nil output") + assert.Contains(err.Error(), "invalid character", "Error message should include the string 'invalid character'") + + return commandOut +} \ No newline at end of file diff --git a/api/blueprint/bootstrapping_api_test.go b/api/blueprint/bootstrapping_api_test.go index 74ba96c..fa4e0b4 100644 --- a/api/blueprint/bootstrapping_api_test.go +++ b/api/blueprint/bootstrapping_api_test.go @@ -1,9 +1,9 @@ package blueprint import ( - "testing" - + "github.com/ingrammicro/concerto/testdata" "github.com/stretchr/testify/assert" + "testing" ) func TestNewBootstrappingServiceNil(t *testing.T) { @@ -13,4 +13,26 @@ func TestNewBootstrappingServiceNil(t *testing.T) { assert.NotNil(err, "Uninitialized service should return error") } -// TODO +func TestGetBootstrappingConfiguration(t *testing.T) { + bcIn := testdata.GetBootstrappingConfigurationData() + GetBootstrappingConfigurationMocked(t, bcIn) + GetBootstrappingConfigurationFailErrMocked(t, bcIn) + GetBootstrappingConfigurationFailStatusMocked(t, bcIn) + GetBootstrappingConfigurationFailJSONMocked(t, bcIn) +} + +func TestReportBootstrappingAppliedConfiguration(t *testing.T) { + bcIn := testdata.GetBootstrappingConfigurationData() + ReportBootstrappingAppliedConfigurationMocked(t, bcIn) + ReportBootstrappingAppliedConfigurationFailErrMocked(t, bcIn) + ReportBootstrappingAppliedConfigurationFailStatusMocked(t, bcIn) + ReportBootstrappingAppliedConfigurationFailJSONMocked(t, bcIn) +} + +func TestReportBootstrappingLog(t *testing.T) { + commandIn := testdata.GetBootstrappingContinuousReportData() + ReportBootstrappingLogMocked(t, commandIn) + ReportBootstrappingLogFailErrMocked(t, commandIn) + ReportBootstrappingLogFailStatusMocked(t, commandIn) + ReportBootstrappingLogFailJSONMocked(t, commandIn) +} \ No newline at end of file diff --git a/testdata/boostrapping_data.go b/testdata/boostrapping_data.go new file mode 100644 index 0000000..4e542ff --- /dev/null +++ b/testdata/boostrapping_data.go @@ -0,0 +1,40 @@ +package testdata + +import ( + "github.com/ingrammicro/concerto/api/types" + "encoding/json" +) + +// GetBootstrappingConfigurationData loads test data +func GetBootstrappingConfigurationData() *types.BootstrappingConfiguration { + + attrs := json.RawMessage(`{"fakeAttribute0":"val0","fakeAttribute1":"val1"}`) + test := types.BootstrappingConfiguration{ + Policyfiles: []types.BootstrappingPolicyfile{ + { + ID: "fakeProfileID0", + RevisionID: "fakeProfileRevisionID0", + DownloadURL: "fakeProfileDownloadURL0", + }, + { + ID: "fakeProfileID1", + RevisionID: "fakeProfileRevisionID1", + DownloadURL: "fakeProfileDownloadURL1", + }, + }, + Attributes: &attrs, + AttributeRevisionID: "fakeAttributeRevisionID", + } + + return &test +} + +// GetBootstrappingContinuousReportData loads test data +func GetBootstrappingContinuousReportData() *types.BootstrappingContinuousReport{ + + testBootstrappingContinuousReport := types.BootstrappingContinuousReport{ + Stdout: "Bootstrap log created", + } + + return &testBootstrappingContinuousReport +}