You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

69 lines
2.0 KiB

package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"testing"
"tera-clone/services/tera-api/internal/serial"
)
type fakeLeaseController struct {
opened []string
failChannel string
}
func (c *fakeLeaseController) Open(_ context.Context, channel string) (io.ReadWriteCloser, error) {
c.opened = append(c.opened, channel)
if channel == c.failChannel {
return nil, errors.New("open failed")
}
return noIOStream{}, nil
}
func (*fakeLeaseController) Status(context.Context) (serial.StreamSnapshot, error) {
return serial.StreamSnapshot{}, nil
}
type noIOStream struct{}
func (noIOStream) Read([]byte) (int, error) { return 0, errors.New("unexpected read") }
func (noIOStream) Write([]byte) (int, error) { return 0, errors.New("unexpected write") }
func (noIOStream) Close() error { return nil }
func TestRunRequiresExplicitConfirmation(t *testing.T) {
if code := run(nil, io.Discard, io.Discard, &fakeLeaseController{}); code != 2 {
t.Fatalf("code = %d, want 2", code)
}
}
func TestRunOpensEveryCH02ThroughCH48WithoutPayload(t *testing.T) {
controller := &fakeLeaseController{}
var stdout bytes.Buffer
code := run([]string{"-operator-confirmed-ch02-ch48-open-close"}, &stdout, io.Discard, controller)
if code != 0 {
t.Fatalf("code = %d; report = %s", code, stdout.String())
}
if len(controller.opened) != 47 || controller.opened[0] != "CH02" || controller.opened[46] != "CH48" {
t.Fatalf("opened = %v", controller.opened)
}
var result report
if err := json.Unmarshal(stdout.Bytes(), &result); err != nil {
t.Fatal(err)
}
if result.Status != "pass" || result.ChannelsPassed != 47 || result.TestPayloadSent || result.DynamicDeviceScan || !result.FixedChannelList {
t.Fatalf("report = %#v", result)
}
}
func TestRunReportsAChannelFailure(t *testing.T) {
controller := &fakeLeaseController{failChannel: "CH24"}
var stdout bytes.Buffer
code := run([]string{"-operator-confirmed-ch02-ch48-open-close"}, &stdout, io.Discard, controller)
if code != 1 {
t.Fatalf("code = %d, want 1", code)
}
}