Skip to content

Commit

Permalink
test exec modem
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bretterklieber committed May 31, 2024
1 parent 4ffd4a7 commit 5802444
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions backend/mmcli/mmcli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,61 @@ func TestMmcli_Exec(t *testing.T) {
})
}
}

func TestMmcli_ExecModem(t *testing.T) {
tests := []struct {
name string
modem string
args []string
expected []byte
expectedErr error
execArgs []any
execReturn []byte
execReturnErr error
}{
{
name: "Success with modem",
modem: "0",
args: []string{"arg1", "arg2"},
expected: []byte("Success"),
execArgs: []any{"-J", "-m", "0", "arg1", "arg2"},
execReturn: []byte("Success"),
},
{
name: "Success without modem",
modem: "",
args: []string{"arg1", "arg2"},
expected: []byte("Success"),
execArgs: []any{"-J", "-m", "/org/freedesktop/ModemManager1/Modem/0", "arg1", "arg2"},
execReturn: []byte("Success"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

mockExec := NewExecCommandMock(t)
if tt.modem == "" {
mockOutput := NewExecCommandOutputMock(t)
mockExec.EXPECT().Execute("bash", "-c", "mmcli -L -J | jq -r '.\"modem-list\"[0]'").Return(mockOutput)
mockOutput.EXPECT().Output().Return([]byte("/org/freedesktop/ModemManager1/Modem/0"), tt.execReturnErr)
}

mockOutput := NewExecCommandOutputMock(t)
mockExec.EXPECT().Execute("mmcli", tt.execArgs...).Return(mockOutput)
mockOutput.EXPECT().Output().Return(tt.execReturn, tt.execReturnErr)

cli := Provide()
cli.exec = mockExec.Execute

got, err := cli.ExecModem(tt.modem, tt.args...)

if tt.expectedErr == nil {
assert.NoError(t, err)
assert.Equal(t, tt.expected, got)
} else {
assert.EqualError(t, err, tt.expectedErr.Error())
}
})
}
}

0 comments on commit 5802444

Please sign in to comment.