blob: 26e7cbea511b37645d5850810fba36aa037c5451 [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// We say that an Environment (or FooSpec) "resolves to" another if the
// properties or constraints given by the former are also given by the latter.
// This gives a partial ordering on environments expressing a subobject
// relationship.
//
// The utility of being able to express that one environment is a
// subenvironment of another lies in validation: an environment is valid if
// it resolves to any member of a golden set of environments we refer to as
// "platforms", as they correspond to the currently available test platforms
// supported by the infrastructure.
//
// The GN environments specified by test authors in the Fuchsia source
// correspond directly to the Environment struct defined here.
package testexec
import (
"encoding/json"
"io/ioutil"
)
// Environment describes the environment a test requires.
type Environment struct {
// Device represents properties of the device that are part of the test
// environment.
Device DeviceSpec `json:"device"`
}
// ResolvesTo gives a partial ordering on environments in which one environment
// resolves to another if the properties given by the former are also given by
// the latter.
func (env Environment) resolvesTo(other Environment) bool {
if !env.Device.resolvesTo(other.Device) {
return false
}
return true
}
// DeviceSpec describes the device environment a test requires.
type DeviceSpec struct {
// Type represents the class of device the test should run on.
// This is a required field.
Type string `json:"type"`
}
func (ds DeviceSpec) resolvesTo(other DeviceSpec) bool {
if ds.Type != "" && ds.Type != other.Type {
return false
}
return true
}
// LoadPlatforms loads the list of test platform environments specified as a
// JSON list at a given filepath.
func LoadPlatforms(platformManifestPath string) ([]Environment, error) {
bytes, err := ioutil.ReadFile(platformManifestPath)
if err != nil {
return nil, err
}
var platforms []Environment
if err = json.Unmarshal(bytes, &platforms); err != nil {
return nil, err
}
return platforms, err
}