Skip to content

Commit

Permalink
Adding test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Oct 17, 2023
1 parent c354cb7 commit 8d069ff
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions pkg/control/v2/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/info"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/details"
"github.com/elastic/elastic-agent/pkg/component"
"github.com/elastic/elastic-agent/pkg/component/runtime"
"github.com/elastic/elastic-agent/pkg/control/v2/cproto"
Expand All @@ -22,11 +23,12 @@ import (
func TestStateMapping(t *testing.T) {

testcases := []struct {
name string
agentState cproto.State
agentMessage string
fleetState cproto.State
fleetMessage string
name string
agentState cproto.State
agentMessage string
fleetState cproto.State
fleetMessage string
upgradeDetails *details.Details
}{
{
name: "waiting first checkin response",
Expand All @@ -49,6 +51,21 @@ func TestStateMapping(t *testing.T) {
fleetState: cproto.State_FAILED,
fleetMessage: "<error value coming from fleet gateway>",
},
{
name: "with upgrade details",
agentState: cproto.State_UPGRADING,
agentMessage: "Upgrading to version 8.13.0",
fleetState: cproto.State_STOPPED,
fleetMessage: "Not enrolled into Fleet",
upgradeDetails: &details.Details{
TargetVersion: "8.13.0",
State: details.StateDownloading,
ActionID: "",
Metadata: details.DetailsMetadata{
DownloadPercent: 1.7,
},
},
},
}

for _, tc := range testcases {
Expand Down Expand Up @@ -101,6 +118,15 @@ func TestStateMapping(t *testing.T) {
},
}

if tc.upgradeDetails != nil {
inputState.UpgradeDetails = &details.Details{
TargetVersion: tc.upgradeDetails.TargetVersion,
State: tc.upgradeDetails.State,
ActionID: tc.upgradeDetails.ActionID,
Metadata: tc.upgradeDetails.Metadata,
}
}

agentInfo := new(info.AgentInfo)

stateResponse, err := stateToProto(inputState, agentInfo)
Expand Down Expand Up @@ -134,7 +160,18 @@ func TestStateMapping(t *testing.T) {
assert.Equal(t, expectedCompState, stateResponse.Components[0])
}

if tc.upgradeDetails != nil {
expectedMetadata := &cproto.UpgradeDetailsMetadata{
ScheduledAt: tc.upgradeDetails.Metadata.ScheduledAt,
DownloadPercent: float32(tc.upgradeDetails.Metadata.DownloadPercent),
FailedState: string(tc.upgradeDetails.Metadata.FailedState),
ErrorMsg: tc.upgradeDetails.Metadata.ErrorMsg,
}
assert.Equal(t, string(tc.upgradeDetails.State), stateResponse.UpgradeDetails.State)
assert.Equal(t, tc.upgradeDetails.TargetVersion, stateResponse.UpgradeDetails.TargetVersion)
assert.Equal(t, tc.upgradeDetails.ActionID, stateResponse.UpgradeDetails.ActionId)
assert.Equal(t, expectedMetadata, stateResponse.UpgradeDetails.Metadata)
}
})
}

}

0 comments on commit 8d069ff

Please sign in to comment.