blob: 8e66e09292d1cd46b7e667eb52a4b4a25d0d7875 [file] [log] [blame]
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package main
import (
pb "chromiumos/vm_tools/vm_rpc"
"flag"
"testing"
)
func TestParseNoArguments(t *testing.T) {
var features Features
fs := flag.NewFlagSet("test", flag.PanicOnError)
fs.Var(&features, "feature", "features to enable")
fs.Parse([]string{})
if len(features.features) != 0 {
t.Fatalf("Expected no features to be set, got: %v", len(features.features))
}
}
func TestParseMultipleArguments(t *testing.T) {
var features Features
fs := flag.NewFlagSet("test", flag.PanicOnError)
fs.Var(&features, "feature", "features to enable")
fs.Parse([]string{"-feature", "UNKNOWN", "-feature", "USED_BY_TESTS"})
if !features.features[pb.StartTerminaRequest_UNKNOWN] {
t.Fatal("Expected UNKNOWN feature to be set, but it wasn't")
}
if !features.IsUsedByTestsEnabled() {
t.Fatal("Expected USED_BY_TESTS feature to be set, but it wasn't")
}
}
func TestString(t *testing.T) {
var features Features
fs := flag.NewFlagSet("test", flag.PanicOnError)
fs.Var(&features, "feature", "features to enable")
fs.Parse([]string{"-feature", "UNKNOWN", "-feature", "USED_BY_TESTS"})
expected := "UNKNOWN,USED_BY_TESTS"
got := features.String()
if got != expected {
t.Fatalf("Expected: \"%s\", got: \"%s\"", expected, got)
}
}
func TestUnknownFlagsAllowed(t *testing.T) {
// Termina ahd Chrome OS versions aren't locked together, so we need to
// ignore any unrecognised flags for compatibility.
var features Features
fs := flag.NewFlagSet("test", flag.PanicOnError)
fs.Var(&features, "feature", "features to enable")
fs.Parse([]string{"-feature", "NOT_A_REAL_FLAG"})
expected := "UNKNOWN" // proto2 maps unrecognised enums to the default.
got := features.String()
if features.String() != expected {
t.Fatalf("Expected: \"%s\" got: \"%s\"", expected, got)
}
}
func TestStartLxd(t *testing.T) {
var features Features
feature := "START_LXD"
fs := flag.NewFlagSet("test", flag.PanicOnError)
fs.Var(&features, "feature", "features to enable")
fs.Parse([]string{"-feature", feature})
if !features.IsStartLxdEnabled() {
t.Fatalf("Expected %s feature to be set, but it wasn't", feature)
}
}
func TestResetLxdOnLaunch(t *testing.T) {
var features Features
feature := "RESET_LXD_ON_LAUNCH"
fs := flag.NewFlagSet("test", flag.PanicOnError)
fs.Var(&features, "feature", "features to enable")
fs.Parse([]string{"-feature", feature})
if !features.IsResetLxdOnLaunchEnabled() {
t.Fatalf("Expected %s feature to be set, but it wasn't", feature)
}
}