Skip to content

Commit

Permalink
Merge pull request #99 from cybozu-go/feature/rm_bmc_restriction
Browse files Browse the repository at this point in the history
Remove restriction of BMC type
  • Loading branch information
ueokande committed Aug 24, 2018
2 parents 8fd1e5b + 66a9461 commit d251066
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 22 deletions.
4 changes: 2 additions & 2 deletions e2e/sabactl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func testSabactlMachines(t *testing.T) {
},
Role: "worker",
BMC: sabakan.MachineBMC{
Type: sabakan.BmcIdrac9,
Type: "iDRAC-9",
},
},
{
Expand All @@ -197,7 +197,7 @@ func testSabactlMachines(t *testing.T) {
},
Role: "boot",
BMC: sabakan.MachineBMC{
Type: sabakan.BmcIpmi2,
Type: "IPMI-2.0",
},
},
}
Expand Down
13 changes: 6 additions & 7 deletions machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import (
"time"
)

const (
// BmcIdrac9 is BMC type for iDRAC-9
BmcIdrac9 = "iDRAC-9"
// BmcIpmi2 is BMC type for IPMI-2.0
BmcIpmi2 = "IPMI-2.0"
)

// MachineState represents a machine's state.
type MachineState string

Expand All @@ -32,6 +25,7 @@ const (

var (
reValidRole = regexp.MustCompile(`^[a-zA-Z][0-9a-zA-Z._-]*$`)
reValidBmcType = regexp.MustCompile(`^[a-z0-9A-Z-_/.]+$`)
reValidLabelVal = regexp.MustCompile(`^[[:print:]]+$`)
reValidLabelName = regexp.MustCompile(`^[a-z0-9A-Z-_/.]+$`)
)
Expand All @@ -41,6 +35,11 @@ func IsValidRole(role string) bool {
return reValidRole.MatchString(role)
}

// IsValidBmcType returns true if role is valid as BMC type
func IsValidBmcType(bmcType string) bool {
return reValidBmcType.MatchString(bmcType)
}

// IsValidLabelName returns true if label name is valid
func IsValidLabelName(name string) bool {
return reValidLabelName.MatchString(name)
Expand Down
18 changes: 17 additions & 1 deletion machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ func TestIsValidRole(t *testing.T) {
}
}

func TestIsValidBmcType(t *testing.T) {
t.Parallel()
validTypes := []string{"validtype1", "valid_type2", "valid/type-3"}
for _, ty := range validTypes {
if !IsValidBmcType(ty) {
t.Error("validator should return true:", ty)
}
}
invalidTypes := []string{"invalid\\type 1", "%invalid+type#2", "^invalid$type?"}
for _, ty := range invalidTypes {
if IsValidBmcType(ty) {
t.Error("validator should return false:", ty)
}
}
}

func TestIsValidLabelName(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -63,7 +79,7 @@ func TestMachine(t *testing.T) {
Serial: "abc",
Rack: 3,
Role: "boot",
BMC: MachineBMC{Type: BmcIpmi2},
BMC: MachineBMC{Type: "IPMI-2.0"},
}

m := NewMachine(spec)
Expand Down
12 changes: 6 additions & 6 deletions models/etcd/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ func TestMachinesIndex(t *testing.T) {
mi := newMachinesIndex()

machines := []*sabakan.Machine{
sabakan.NewMachine(sabakan.MachineSpec{Serial: "1", Labels: map[string]string{"product": "R630", "datacenter": "ty3"}, Role: "boot", BMC: sabakan.MachineBMC{Type: sabakan.BmcIpmi2}}),
sabakan.NewMachine(sabakan.MachineSpec{Serial: "2", Labels: map[string]string{"product": "R630", "datacenter": "ty3"}, Role: "worker", BMC: sabakan.MachineBMC{Type: sabakan.BmcIpmi2}}),
sabakan.NewMachine(sabakan.MachineSpec{Serial: "3", Labels: map[string]string{"product": "R730xd", "datacenter": "ty3"}, Role: "worker", BMC: sabakan.MachineBMC{Type: sabakan.BmcIpmi2}}),
sabakan.NewMachine(sabakan.MachineSpec{Serial: "1", Labels: map[string]string{"product": "R630", "datacenter": "ty3"}, Role: "boot", BMC: sabakan.MachineBMC{Type: "IPMI-2.0"}}),
sabakan.NewMachine(sabakan.MachineSpec{Serial: "2", Labels: map[string]string{"product": "R630", "datacenter": "ty3"}, Role: "worker", BMC: sabakan.MachineBMC{Type: "IPMI-2.0"}}),
sabakan.NewMachine(sabakan.MachineSpec{Serial: "3", Labels: map[string]string{"product": "R730xd", "datacenter": "ty3"}, Role: "worker", BMC: sabakan.MachineBMC{Type: "IPMI-2.0"}}),
}

for _, m := range machines {
Expand All @@ -38,7 +38,7 @@ func TestMachinesIndex(t *testing.T) {
"datacenter": "ty3",
},
Role: "worker",
BMC: sabakan.MachineBMC{Type: sabakan.BmcIpmi2},
BMC: sabakan.MachineBMC{Type: "IPMI-2.0"},
})
current := sabakan.NewMachine(
sabakan.MachineSpec{
Expand All @@ -48,7 +48,7 @@ func TestMachinesIndex(t *testing.T) {
"datacenter": "ty3",
},
Role: "worker",
BMC: sabakan.MachineBMC{Type: sabakan.BmcIpmi2},
BMC: sabakan.MachineBMC{Type: "IPMI-2.0"},
})
current.Status.State = sabakan.StateRetiring

Expand Down Expand Up @@ -76,7 +76,7 @@ func TestMachinesIndex(t *testing.T) {
"datacenter": "ty3",
},
Role: "worker",
BMC: sabakan.MachineBMC{Type: sabakan.BmcIpmi2},
BMC: sabakan.MachineBMC{Type: "IPMI-2.0"},
}))

serials = mi.query(sabakan.Query{"labels": "product=R730xd"})
Expand Down
4 changes: 2 additions & 2 deletions web/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (s Server) handleMachinesPost(w http.ResponseWriter, r *http.Request) {
renderError(r.Context(), w, BadRequest("BMC type is empty"))
return
}
if (m.BMC.Type != sabakan.BmcIpmi2) && (m.BMC.Type != sabakan.BmcIdrac9) {
renderError(r.Context(), w, BadRequest("unknown BMC type"))
if !sabakan.IsValidBmcType(m.BMC.Type) {
renderError(r.Context(), w, BadRequest("BMC type contains invalid character"))
return
}
m.IPv4 = nil
Expand Down
8 changes: 4 additions & 4 deletions web/machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func testMachinesPost(t *testing.T) {
},
"rack": 1,
"role": "boot",
"bmc": {"type": "unknown-BMC"}
"bmc": {"type": "&invalid$ bmc#type+"}
}]`, http.StatusBadRequest},
{`[{
"serial": "2222abcd",
Expand Down Expand Up @@ -185,7 +185,7 @@ func testMachinesGet(t *testing.T) {
},
Rack: 1,
Role: "boot",
BMC: sabakan.MachineBMC{Type: sabakan.BmcIdrac9},
BMC: sabakan.MachineBMC{Type: "iDRAC-9"},
}),
sabakan.NewMachine(sabakan.MachineSpec{
Serial: "5678abcd",
Expand All @@ -195,7 +195,7 @@ func testMachinesGet(t *testing.T) {
},
Rack: 1,
Role: "worker",
BMC: sabakan.MachineBMC{Type: sabakan.BmcIdrac9},
BMC: sabakan.MachineBMC{Type: "iDRAC-9"},
}),
sabakan.NewMachine(sabakan.MachineSpec{
Serial: "1234efgh",
Expand All @@ -205,7 +205,7 @@ func testMachinesGet(t *testing.T) {
},
Rack: 2,
Role: "boot",
BMC: sabakan.MachineBMC{Type: sabakan.BmcIpmi2},
BMC: sabakan.MachineBMC{Type: "IPMI-2.0"},
}),
})

Expand Down

0 comments on commit d251066

Please sign in to comment.