Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup Associate Validation #7

Closed
wants to merge 11 commits into from
4 changes: 3 additions & 1 deletion bin/terraorg
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ACTIONS = [
].freeze

STRICT_VALIDATION = ENV.fetch('TERRAORG_STRICT_VALIDATION', 'true')
ALLOW_ORPHANED_ASSOCIATES = ENV.fetch('ALLOW_ORPHANED_ASSOCIATES', 'false')
SQUADS_FILE = ENV.fetch('TERRAORG_SQUADS', 'squads.json')
PLATOONS_FILE = ENV.fetch('TERRAORG_PLATOONS', 'platoons.json')
ORG_FILE = ENV.fetch('TERRAORG_ROOT', 'org.json')
Expand Down Expand Up @@ -97,7 +98,8 @@ org_data = File.read(ORG_FILE)
org = Org.new(JSON.parse(org_data), platoons, squads, people, GSUITE_DOMAIN)

strict = (STRICT_VALIDATION == 'true')
org.validate!(strict: strict)
allow_orphaned_associates = (ALLOW_ORPHANED_ASSOCIATES == 'true')
org.validate!(strict: strict, allow_orphaned_associates)

case action
when 'generate-squads-md'
Expand Down
12 changes: 7 additions & 5 deletions lib/terraorg/model/org.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def initialize(parsed_data, platoons, squads, people, gsuite_domain)
@squads = squads
end

def validate!(strict: true)
def validate!(strict: true, allow_orhpaned_associates: false)
zcicala marked this conversation as resolved.
Show resolved Hide resolved
failure = false

# Do not allow the JSON files to contain any people who have left.
Expand Down Expand Up @@ -134,10 +134,12 @@ def validate!(strict: true)
end

# Validate that any associate is a member of some squad
associates_but_not_members = Set.new(all_associates.map(&:id)) - Set.new(all_members.map(&:id)) - exceptions
if !associates_but_not_members.empty?
$stderr.puts "ERROR: #{associates_but_not_members} are associates of squads but not members of any squad"
failure = true
if !allow_orhpaned_associates
zcicala marked this conversation as resolved.
Show resolved Hide resolved
associates_but_not_members = Set.new(all_associates.map(&:id)) - Set.new(all_members.map(&:id)) - exceptions
if !associates_but_not_members.empty?
$stderr.puts "ERROR: #{associates_but_not_members.to_a().join()} are associates of squads but not members of any squad"
zcicala marked this conversation as resolved.
Show resolved Hide resolved
failure = true
end
end

raise "CRITICAL: Validation failed due to at least one error above" if failure && strict
Expand Down
2 changes: 1 addition & 1 deletion lib/terraorg/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
# limitations under the License.

module Terraorg
VERSION = '0.5.0'
VERSION = '0.5.1'
end
286 changes: 274 additions & 12 deletions test/terraorg/model/org_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,14 @@

class OrgTest < Minitest::Test
def setup
gsuite_domain = "gsuite"
slack_domain = "slack"
people = People.new
squads_data = File.read('./examples/squads.json')
squads = Squads.new(JSON.parse(squads_data), people, gsuite_domain, slack_domain)
platoons_data = File.read('./examples/platoons.json')
platoons = Platoons.new(JSON.parse(platoons_data), squads, people, gsuite_domain)
org_data = File.read('./examples/org.json')
@org = Org.new(JSON.parse(org_data), platoons, squads, people, gsuite_domain)


end


def test_platoons_tf_generation
tf = @org.generate_tf_platoons
org = generate_org($WORKING_ORG, $WORKING_PLATOONS, $WORKING_SQUADS)
tf = org.generate_tf_platoons

#Make sure not null
assert tf != nil
Expand All @@ -40,7 +33,8 @@ def test_platoons_tf_generation
end

def test_org_tf_generation
tf = @org.generate_tf_org
org = generate_org($WORKING_ORG, $WORKING_PLATOONS, $WORKING_SQUADS)
tf = org.generate_tf_org

#Make sure not null
assert tf != nil
Expand All @@ -53,18 +47,286 @@ def test_org_tf_generation
end

def test_files_written
@org.generate_tf
org = generate_org($WORKING_ORG, $WORKING_PLATOONS, $WORKING_SQUADS)
tf = org.generate_tf

#Make sure we're generating the files we expect to generate
assert File.exist?('auto.platoons.tf')
assert File.exist?('auto.exception_squads.tf')
assert File.exist?('auto.org.tf')
end

def test_associate_without_squad
org = generate_org($WORKING_ORG, $WORKING_PLATOONS, $ORPHANED_ASSOCIATE_SQUADS)
assert_output('', "ERROR: associate1 are associates of squads but not members of any squad\n") {
org.validate!(strict: false)
}

assert_output('', "") {
org.validate!(strict: false, allow_orhpaned_associates: true)
}

end

def teardown
FileUtils.rm_f('auto.platoons.tf') if File.exist?('auto.platoons.tf')
FileUtils.rm_f('auto.exception_squads.tf') if File.exist?('auto.exception_squads.tf')
FileUtils.rm_f('auto.org.tf') if File.exist?('auto.org.tf')
end

def generate_org(org_data, platoons_data, squads_data)
gsuite_domain = "gsuite"
slack_domain = "slack"
people = People.new
squads = Squads.new(JSON.parse(squads_data), people, gsuite_domain, slack_domain)
platoons = Platoons.new(JSON.parse(platoons_data), squads, people, gsuite_domain)
return Org.new(JSON.parse(org_data), platoons, squads, people, gsuite_domain)
end

$WORKING_ORG = <<-JSON
{
"exception_people": [
{
"location": "US",
"members": [
"exception1"
]
}
],
"exception_squads": [],
"id": "contoso",
"manager": "rchen",
joshk0 marked this conversation as resolved.
Show resolved Hide resolved
"manager_location": "US",
"metadata": {},
"name": "Contoso",
"platoons": [
"sprockets",
"widgets"
],
"version": "v1"
}
JSON

$WORKING_PLATOONS = <<-JSON
{
"platoons": [
{
"id": "sprockets",
"manager": "manager2",
"name": "Sprockets",
"squads": [
"legal",
"logistics"
]
},
{
"id": "widgets",
"manager": "manager1",
"name": "Widgets",
"squads": [
"marketing",
"sales"
]
}
],
"version": "v1"
}

JSON

$WORKING_SQUADS = <<-JSON
{
"squads": [
{
"id": "legal",
"metadata": {
"epo": "epo4",
"manager": "manager5",
"pm": [
"pm1"
],
"slack": "#legal",
"sme": "sme8"
},
"name": "Legal",
"team": [
{
"location": "US",
"members": [
"member1",
"member2"
]
}
]
},
{
"id": "logistics",
"metadata": {
"epo": "epo9",
"manager": "manager11",
"pm": [
"pm9"
],
"slack": "#logistics",
"sme": "sme9"
},
"name": "Logistics",
"team": [
{
"location": "CN",
"members": [
"member55",
"member56"
]
}
]
},
{
"id": "marketing",
"metadata": {
"manager": "manager2",
"pm": [
"pm2"
],
"slack": "#marketing",
"sme": "sme2"
},
"name": "Marketing",
"team": [
{
"location": "FR",
"members": [
"member3",
"member4"
]
}
]
},
{
"id": "sales",
"metadata": {
"epo": "epo1",
"manager": "epo2",
"pm": [
"pm1"
],
"slack": "#sales",
"sme": "sme1"
},
"name": "Sales",
"team": [
{
"location": "GB",
"members": [
"member8",
"member9"
]
}
]
}
],
"version": "v1"
}

JSON

$ORPHANED_ASSOCIATE_SQUADS = <<-JSON
joshk0 marked this conversation as resolved.
Show resolved Hide resolved
{
"squads": [
{
"id": "legal",
"metadata": {
"epo": "epo4",
"manager": "manager5",
"pm": [
"pm1"
],
"slack": "#legal",
"sme": "sme8"
},
"name": "Legal",
"team": [
{
"location": "US",
"members": [
"member1",
"member2"
]
}
]
},
{
"id": "logistics",
"metadata": {
"epo": "epo9",
"manager": "manager11",
"pm": [
"pm9"
],
"slack": "#logistics",
"sme": "sme9"
},
"name": "Logistics",
"team": [
{
"location": "CN",
"members": [
"member55",
"member56"
]
}
]
},
{
"id": "marketing",
"metadata": {
"manager": "manager2",
"pm": [
"pm2"
],
"slack": "#marketing",
"sme": "sme2"
},
"name": "Marketing",
"team": [
{
"location": "FR",
"associates":[
"associate1"
],
"members": [
"member3",
"member4"
]
}
]
},
{
"id": "sales",
"metadata": {
"epo": "epo1",
"manager": "epo2",
"pm": [
"pm1"
],
"slack": "#sales",
"sme": "sme1"
},
"name": "Sales",
"team": [
{
"location": "GB",
"members": [
"member8",
"member9"
]
}
]
}
],
"version": "v1"
}

JSON

end