# Unit tests for block device configuration in user-data script
# These tests validate block device setup renders correctly

variables {
  chef_role         = "test_role"
  aws_iam_role_id   = "test-iam-role"
  aws_instance_name = "test-instance"
}

# Test: EBS mount point appears in script when configured
run "ebs_mount_point_in_user_data" {
  command = plan

  variables {
    ebs_data_mount_point = "/data"
  }

  assert {
    condition     = can(regex("/data", output.user_data_output))
    error_message = "EBS mount point /data should appear in user data"
  }

  assert {
    condition     = can(regex("Adding EBS device to fstab at /data", output.user_data_output))
    error_message = "User data should log EBS fstab addition"
  }
}

# Test: Instance store mount point appears in script when configured
run "instance_store_mount_point_in_user_data" {
  command = plan

  variables {
    instance_store_mount_point = "/scratch"
  }

  assert {
    condition     = can(regex("/scratch", output.user_data_output))
    error_message = "Instance store mount point /scratch should appear in user data"
  }

  assert {
    condition     = can(regex("Adding instance store device to fstab at /scratch", output.user_data_output))
    error_message = "User data should log instance store fstab addition"
  }
}

# Test: Both mount points appear when both configured
run "both_mount_points_in_user_data" {
  command = plan

  variables {
    ebs_data_mount_point       = "/data"
    instance_store_mount_point = "/scratch"
  }

  assert {
    condition     = can(regex("/data", output.user_data_output))
    error_message = "EBS mount point should appear in user data"
  }

  assert {
    condition     = can(regex("/scratch", output.user_data_output))
    error_message = "Instance store mount point should appear in user data"
  }
}

# Test: XFS filesystem triggers xfsprogs installation
run "xfs_installs_xfsprogs" {
  command = plan

  variables {
    ebs_data_mount_point         = "/data"
    block_device_filesystem_type = "xfs"
  }

  assert {
    condition     = can(regex("xfsprogs", output.user_data_output))
    error_message = "XFS filesystem type should trigger xfsprogs installation"
  }

  assert {
    condition     = can(regex("mkfs\\.xfs", output.user_data_output))
    error_message = "XFS filesystem type should use mkfs.xfs"
  }
}

# Test: ext4 filesystem uses correct mkfs command
run "ext4_uses_correct_mkfs" {
  command = plan

  variables {
    ebs_data_mount_point         = "/data"
    block_device_filesystem_type = "ext4"
  }

  assert {
    condition     = can(regex("mkfs\\.ext4", output.user_data_output))
    error_message = "ext4 filesystem type should use mkfs.ext4"
  }
}

# Test: fstab entries include nofail option
run "fstab_includes_nofail" {
  command = plan

  variables {
    ebs_data_mount_point = "/data"
  }

  assert {
    condition     = can(regex("nofail", output.user_data_output))
    error_message = "fstab entries must include nofail option"
  }
}

# Test: fstab entries include noatime option
run "fstab_includes_noatime" {
  command = plan

  variables {
    ebs_data_mount_point = "/data"
  }

  assert {
    condition     = can(regex("noatime", output.user_data_output))
    error_message = "fstab entries must include noatime option for performance"
  }
}

# Test: Instance store fstab includes discard option
run "instance_store_fstab_includes_discard" {
  command = plan

  variables {
    instance_store_mount_point = "/scratch"
  }

  assert {
    condition     = can(regex("discard", output.user_data_output))
    error_message = "Instance store fstab entry must include discard option"
  }
}

# Test: Instance store fstab includes systemd device timeout
run "instance_store_fstab_includes_device_timeout" {
  command = plan

  variables {
    instance_store_mount_point = "/scratch"
  }

  assert {
    condition     = can(regex("x-systemd\\.device-timeout", output.user_data_output))
    error_message = "Instance store fstab entry must include x-systemd.device-timeout"
  }
}

# Test: Block device discovery uses correct model strings
run "block_device_discovery_model_strings" {
  command = plan

  variables {
    ebs_data_mount_point = "/data"
  }

  assert {
    condition     = can(regex("Amazon Elastic Block Store", output.user_data_output))
    error_message = "Block device discovery must check for EBS model string"
  }

  assert {
    condition     = can(regex("Amazon EC2 NVMe Instance Storage", output.user_data_output))
    error_message = "Block device discovery must check for instance store model string"
  }
}

# Test: Block device formatting has error handling
run "block_device_formatting_error_handling" {
  command = plan

  variables {
    ebs_data_mount_point = "/data"
  }

  assert {
    condition     = can(regex("FATAL: Failed to download Cinc client", output.user_data_output))
    error_message = "User data must have error handling for Cinc download"
  }

  assert {
    condition     = can(regex("FATAL: Failed to install Cinc client", output.user_data_output))
    error_message = "User data must have error handling for Cinc install"
  }
}

# Test: Uses blkid to check for existing filesystem
run "checks_existing_filesystem" {
  command = plan

  variables {
    ebs_data_mount_point = "/data"
  }

  assert {
    condition     = can(regex("blkid", output.user_data_output))
    error_message = "User data must use blkid to check for existing filesystem"
  }

  assert {
    condition     = can(regex("already has a filesystem, skipping format", output.user_data_output))
    error_message = "User data must skip formatting if filesystem exists"
  }
}

# Test: Uses UUID for fstab entries
run "fstab_uses_uuid" {
  command = plan

  variables {
    ebs_data_mount_point = "/data"
  }

  assert {
    condition     = can(regex("UUID=", output.user_data_output))
    error_message = "fstab entries must use UUID for device identification"
  }

  assert {
    condition     = can(regex("blkid -s UUID -o value", output.user_data_output))
    error_message = "User data must extract UUID using blkid"
  }
}

# Test: No block device config when no mount points specified
run "no_block_device_config_when_empty" {
  command = plan

  variables {
    ebs_data_mount_point       = ""
    instance_store_mount_point = ""
  }

  # The block device section should still be present but inactive
  # when no mount points are configured
  assert {
    condition     = can(regex("Block Device Configuration", output.user_data_output))
    error_message = "Block device configuration section should exist in script"
  }
}
